binary representation - shortcuts n negation x + x = 1111…1111 two = -1 (in 2’s complement)...

6
Binary Representation - Shortcuts Negation x + x = 1111…1111 two = -1 (in 2’s complement) Therefore, -x = x + 1 Sign Extension o Positive numbers : easy o Negative numbers (2’s complement): - 4 in 4 bits : 1100 - 4 in 5 bits : 11100 Why is this useful? 1. Actual sign extension 2. Binary to decimal conversion (2’s complement) -- can forget about leading 1’s e.g. 1111 1110 two = 110 two = -2 ten

Upload: gladys-potter

Post on 24-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Binary Representation - Shortcuts n Negation x + x = 1111…1111 two = -1 (in 2’s complement) Therefore, -x = x + 1 n Sign Extension o Positive numbers :

Binary Representation - Shortcuts

Negation x + x = 1111…1111two = -1 (in 2’s complement) Therefore, -x = x + 1

Sign Extension o Positive numbers : easy o Negative numbers (2’s complement): - 4 in 4 bits : 1100 - 4 in 5 bits : 11100 Why is this useful? 1. Actual sign extension 2. Binary to decimal conversion (2’s complement) -- can forget about leading 1’s e.g. 1111 1110two = 110two = -2ten

Page 2: Binary Representation - Shortcuts n Negation x + x = 1111…1111 two = -1 (in 2’s complement) Therefore, -x = x + 1 n Sign Extension o Positive numbers :

Negative Binary to Decimal

Change sign (2’s compliment) 1111 1111 1011 0100two

= - 0000 0000 0100 1100two

= - 76

Changing signWhat pattern appears at the right end ? 1 followed by some (or no) 0’s

o Trick: retain that pattern and flip everything to the left of it

e.g. - 1111 1100 0000 0000 = 0000 0100 0000 0000

Page 3: Binary Representation - Shortcuts n Negation x + x = 1111…1111 two = -1 (in 2’s complement) Therefore, -x = x + 1 n Sign Extension o Positive numbers :

A Feel for Base 2

What is multiplication by 2 ? 5 x 2 = 10ten

101two x 2 = ?(can you use this in efficiently solving 4.11?)

What is division by 2, ignoring any remainder? 0000 0101two / 2 = ? 1110 0010two / 2 = ?

What does a multiple of 4 look like? 4 = 0000 0100 24 = 0001 1000 60 = 0011 1100 All end in 2 zeros. Why?

Page 4: Binary Representation - Shortcuts n Negation x + x = 1111…1111 two = -1 (in 2’s complement) Therefore, -x = x + 1 n Sign Extension o Positive numbers :

“Translate Binary to Hexadecimal”

Binary and Hex are two different representations Can convert from one to the other treating numbers as a series of bits Need not worry about signed/unsigned, 2’s complement, 1’s

complement, etc

Make groups of 4 bits, starting from the right Add 0’s to the left if necessary (do not sign extend) Translate each group of 4 bits to hex individually

e.g. 01110000100101two = 0001 1100 0010 0101two = 1c25hex

Page 5: Binary Representation - Shortcuts n Negation x + x = 1111…1111 two = -1 (in 2’s complement) Therefore, -x = x + 1 n Sign Extension o Positive numbers :

“Bits have no inherent meaning”

“Value” of a binary number depends on how you look at it e.g. consider the 4 bit number 1110two

unsigned : 14 signed : ??? (can assume 2’s complement) 2’s complement : - (0010two) = -2

1’s complement : - (0001two) = -1

sign and magnitude : - (110two) = -6

Page 6: Binary Representation - Shortcuts n Negation x + x = 1111…1111 two = -1 (in 2’s complement) Therefore, -x = x + 1 n Sign Extension o Positive numbers :

Overflow and Underflow

Assume 4 bit numbers in 2’s complement form Range : - 23 to (23 - 1), or - 8 to 7 7 + 5 = 12 ……… cannot represent 12 using 4 bits (signed) !

What happens in binary addition ? 0111(7) + 0101(5) = 1100(-4) The number “appears” to be negative because it is out of range

Similarly, 7 - (-5) = 12 ……. out of range Underflow: (-7) + (-5) = -12 ……. negative number out of range Remember : overflow/underflow can occur only when

1. Numbers of same sign are added 2. Numbers of opposite signs are subtracted