Binary Representation

Download Report

Transcript Binary Representation

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
Negative Binary to Decimal

Change sign (2’s compliment)
1111 1111 1011 0100two
= - 0000 0000 0100 1100two
= - 76
Changing sign
What 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
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?
“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
“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
signed
2’s complement
1’s complement
sign and magnitude
:
:
:
:
:
14
??? (can assume 2’s complement)
- (0010two) = -2
- (0001two) = -1
- (110two) = -6
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

