270 likes | 1.04k Views
2’s Complement. Another system that lets us represent negative numbers MSB is STILL the sign bit, but there is no negative zero Negative numbers count backwards and wrap around Calculating 2’s complement (Pos Neg) Flip the bits ( 0 1 and 10 ) Add 1. Example ( Pos Neg ).
E N D
2’s Complement • Another system that lets us represent negative numbers • MSB is STILL the sign bit, but there is no negative zero • Negative numbers count backwards and wrap around • Calculating 2’s complement (Pos Neg) • Flip the bits ( 01 and 10 ) • Add 1
Example ( Pos Neg ) 110 -110 00012 11112 • Flip bits: 1110 • Add 1: 1110 +0001 1111
Another Example ( Pos Neg ) 2510 -2510 000110012 111001112 • Flip bits: 11100110 • Add 1: 11100110 +00000001 11100111
Your Turn • Assuming an 8-bit restriction, what is -2110 in 2’s complement? • Flip bits • Add 1 Answer: 111010112
Your Turn • Assuming an 8-bit restriction, what is -3010 in 2’s complement? • Flip bits • Add 1 Answer: 111000102
Example ( Neg Pos ) -410 410 11002 01002 • Flip bits: 0011 • Add 1: 0011 +0001 0100
Another Example ( Neg Pos ) -2910 2910 111000112 000111012 • Flip bits: 00011100 • Add 1: 00011100 +00000001 00011101
Your Turn • Assuming 2’s complement, what is the decimal value of 111110012? • Flip bits • Add 1 Answer: -710
Your Turn • Assuming 2’s complement, what is the decimal value of 111010102? • Flip bits • Add 1 Answer: 22
SHORTCUT! • Find the 1 on the farthest right • Flip all the bits to the left of the 1 (DO NOT FLIP THE 1) Example: 4210 -4210 001010102 110101102
Awesomeness of 2’s Complement • No more negative zero • Lower minimum value: -(2N-1) • So what’s the big deal? • Everything is addition • No need for special hardware to do subtraction
2’s Complement Addition • Just like normal positive binary addition • You MUST restrict the number of bits • IGNORE any overflow bits • maintain bit-size restriction
Positive Addition Example 1210 + 410 = 1610 Assuming 2’s complement 000010102 1210 +000000102+ 410 000011002 1610
Negative Addition Example -1210 + -410 = -1610 111101002 -1210 +111111002+ -410 111100002 -1610 NOTE: We ignored the last overflow bit on the left!
Your Turn • Show the binary addition of -14 + -3 = -17
Subtraction Example 1610 – 410 = 1610 + -410 = 1210 000100002 +111111002 000011002 NOTE: We ignored the last overflow bit on the left!
Your Turn • Show the binary subtraction of 23 – 10 = 13
Overflow / Underflow Problem • Addition and bit-size restriction allow for possible overflow / underflow • Overflow – when the addition of two binary numbers yields a result that is greater than the maximum possible value • Underflow – when the addition/subtraction of two binary numbers yields a result that is less than the minimum possible value
Overflow Example • Assume 4-bit restriction and 2’s complement • Maximum possible value: 24-1 – 1 = 7 610 + 310 = 910 01102 610 +00112+310 10012 -710 not good!
Underflow Example • Assume 4-bit restriction and 2’s complement • Minimum possible value: -(24-1) = -8 -510 + -510 = -1010 10112 -510 +10112+-510 01102 610 not good!