Negative Number SignMagnitude leftmost bit as the sign
Negative Number • Sign-Magnitude: left-most bit as the sign bit – 16 bits – Example: 4 -bit numbers +510 is given by 01012 -510 is given by 11012 • 2’s complement: 16 bits: – Example: 4 -bit numbers +510 is given by 01012’ -510 is given by 10112’
Lecture 9 notes • • • Negative number Sign-magnitude representation 2’s complement Binary addition Binary subtraction
Convert (156) 10 to (? ) 2’ • 2’s complement representation of positive numbers is the same as signmagnitude representation. • 16 bits example: • 156= 128 + 16 + 8 + 4 = 1* 27+ 1* 24 + 1* 23 + 1* 22 = (0000000010011100) 2’
Convert (-156) 10 to (? ) 2’ • Step 1: ignore the negative sign, obtain the 2’complement of the positive value: (156) 10 = (000010011100) • Step 2: Bitwise inverse: =(111101100011) 2 • Step 3: add 1 using binary addition =(111101100100) 2’ • done
Remarks • Representation of negative number is always associated with the context of total bits • With 8 bits, -2 = (11111110) 2’ • With 16 bits, -2 = (111111110) 2’
Binary addition review x 1 0 x Y 1 0 1 y 1 0 0 sum bit Example: + 0 1 1 1 0 0 1 0 1 0 0 0 carry bit 1 0 1 0 1 0
Binary Subtraction • When represented in sign-magnitude format, subtraction is performed in a similar way as in the base 10 case. • Subtraction uses a different set of `rules’ other than the addition • With 2’s complement representation, we can achieve subtraction via binary addition!
Example 7 -6 • Step 1: get 2’ complement representation for 7 and – 6: 7 = (00000111) 2’ -6=(11111010) 2’ * ** • Perform binary addition between * and **, we get (1 00000001) • Ignore the overflow bit, we have (00000001) • Done, this is the result of the subtraction represented in 2’s complement format.
- Slides: 8