30
1 Lecture 2 Complement Floating Point Number Character Encoding

1 Lecture 2 Complement Floating Point Number Character Encoding

Embed Size (px)

Citation preview

Page 1: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

1

Lecture 2

Complement Floating Point Number Character Encoding

Page 2: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

2

Complement

Complement as a representation of negative number More commonly used

1’s complement 2’s complement (used in Personal Computer)

Page 3: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

3

Complement (for binary)

1’s complement 1’s complement of a number is just to change 0 to 1

and 1 to 0. e.g. 10 0100 => 1’s complement is 01 1011

2’s complement 2’s complement of a number is just to add 1 to

its 1’s complement e.g. 10 0100 => 2’s complement is 01 1100

Page 4: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

4

Example - Complement

Consider the binary number 100 0110 it’s 1’s complement is 011 1001

it’s 2’s complement is 011 1010

How to differentiate whether a binary number is positive or negative?

* Use a leading zero for positive number! Why?

Page 5: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

5

1’s complement Arithmetic

Positive Number + Negative Number Result is a positive number Result is a negative number

Positive Number + Positive Number Overflow (explain in slide 24)

Negative Number + Negative Number Underflow (explain in slide 25)Note:Convert the negative number to 1’s complement before any

calculatione.g. 10 + (-8) => 0 1010 + 1 0111

1’s complement of 0 1000

Page 6: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

6

1’s Complement Arithmetic

Positive Number + Negative Number Result is a positive number (rotate)

e.g. 10 + (-8)

0 1010 0 0001

+ 1 0111 + 1

10 0001 0 0010

RotateAnswer is 210

Page 7: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

7

1’s Complement Arithmetic

Positive Number + Negative Number Result is a positive number (Rotate) Result is a negative number (How do you know the

result is negative? )

Example8 + (-10)

0 1000+ 1 0101

1 1101

Negative Number (used 1’s complement to get the value)

Page 8: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

8

1’s Complement Arithmetic

Positive Number + Positive Number Overflow

Example

8 + 9 = 17

0 1000

+ 0 1001

1 0001 => -14

It may be wrongly interpreted as a negative number. So, we call this case as overflow.

Page 9: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

9

1’s Complement Arithmetic

Negative Number + Negative Number underflow

Example

(-8) + (-9) = -17

1 0111

+ 1 0110

0 1001 => 9

It may be wrongly interpreted as a positive number. So, we call this case as underflow.

Page 10: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

10

2’s complement

How to calculate 2’s complement. Add 1 to 1’s complement

1110 0011 1’s Complement 0001 1100 2’s Complement 0001 1101

Page 11: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

11

2’s complement Arithmetic

Positive Number + Negative Number Result is a positive number Result is a negative number

Positive Number + Positive Number Overflow

Negative Number + Negative Number UnderflowNote:Convert the negative number to 1’s complement before any

calculatione.g. 10 + (-8) => 0 1010 + 1 1000

2’s complement of 0 1000

Page 12: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

12

2’s Complement Arithmetic

Positive Number + Negative Number Result is a positive number (Discard)

e.g. 10 + (-8)

0 1010

+ 1 1000

10 0010

Discard

0 0010

Answer is 210.

Page 13: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

13

2’s Complement Arithmetic

Positive Number + Negative Number

Result is a negative numberExample

8 + (-10)

0 1000

+ 1 0110

1 1110

Negative Number (used 2’s complement to get the value)

Page 14: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

14

2’s Complement Arithmetic

Positive Number + Positive Number Overflow

Example

8 + 9 = 17

0 1000

+ 0 1001

1 0001 => -15

It may be wrongly interpreted as a negative number. So, we call this case as overflow.

Page 15: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

15

2’s Complement Arithmetic

Negative Number + Negative Number underflow

Example

(-8) + (-9) = -17

1 1000

+ 1 0111

0 1111 => 15

It may be wrongly interpreted as a positive number. So, we call this case as underflow.

Page 16: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

16

Example - Arithmetic with complement

Example for 1’s complement arithmetic

Example 1 Example 2 Example 3

Page 17: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

17

Example - Arithmetic with complement

Example for 2’s complement arithmetic

Example 1 Example 2 Example 3

Page 18: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

18

Floating-point Numbers

IEEE Standard 754 Floating Point Representation Represent real number in scientific notation Represent a number with a base and exponent i.e. 0.00001101 => 1.101 x 2 -5

base exponent

Page 19: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

19

Floating-point Number

Advantages Possible to represent a very large and very small

number e.g. 10+100 10-100

It will not lose precision (due to round-off) CPU will have instruction (low level command to

support floating point calculation – e.g. co-processor)

Page 20: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

20

Floating-point Number

Format Three basic components: the sign, the

exponent [指數 ] and the mantissa [尾數 ]

For a 32-bit floating point number

Page 21: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

21

Floating-point Number

Sign Bit Zero denotes a positive number; one denotes

a negative number. Exponent

Bias is used (so we can differentiate between positive and negative)

e.g. for 32 bits, bias is -127 and for 64 bits bias is 1023. (???????Bias)

exponent > 127 ==> positive exponent exponent < 127 ==> negative exponent

*12710 = 0111 11112

Page 22: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

22

Floating-point Number

Mantissa It must be normalized (i.e. it must in the format

of 1.XXXX) So, the one before the decimal can be

assumed. e.g. 100.100 mantissa is 00100 Do you know why?

Exponent => 2 so mantissa is 1.00100 Assume the 1 before decimal => 00100

Check the 64-bit floating point standard yourselves!

Page 23: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

23

Floating Point Number

Steps

1. Convert the number to binary

2. Convert the binary number to normalized form

3. Find out the sign, exponent and mantissa

Page 24: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

24

Floating-point Number

Example Number: +101011.0101

Sign bit : 0

Normalized Mantissa (23 bits): (E.g. 1.010110101) 010 1101 0100 0000 0000 0000

Exponent (8 bits): 1000 0100 (127 + 5 = 13210)

Floating Point:

0 1000 0100 010 1101 0100 0000 0000 0000

Sign Exponent Mantissa

Page 25: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

25

Character Representation

Binary Code Decimal ‘0’ - ‘9’ by 4-bits code Easy conversion to decimal digits for printing

or display Faster decimal calculations

Extended Binary Coded Decimal Code (EBCDIC) 8-bits code to represent more characters

Page 26: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

26

Character Representation

American Standard Code for Information Interchange (ASCII) - 7 bits Define a vendor-independent character coding system (including some control character e.g. Esc code for printer) Support data communication between computers and peripheral / telecommunication device

Note:

The character will be stored in form of ASCII code

Page 27: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

27

ASCII Table

Page 28: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

28

Multinational Character

Korea, Japan, Chinese and Vietnamese Characters Unicode (Version 5.1) (CKJV)

http://www.unicode.org/versions/Unicode5.1.0/

Big5 (Taiwan) for Traditional Chinese Character GB (China) code for Simplified Chinese character

Page 29: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

29

Character Encoding

Page 30: 1 Lecture 2  Complement  Floating Point Number  Character Encoding

30

Unicode

Start Program Accessories System Tools Character Map