1 / 21

2’s Complement

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 10 ) Add 1. Example ( Pos  Neg ).

morton
Download Presentation

2’s Complement

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 10 ) • Add 1

  2. Example ( Pos  Neg ) 110 -110 00012 11112 • Flip bits: 1110 • Add 1: 1110 +0001 1111

  3. Another Example ( Pos  Neg ) 2510 -2510 000110012 111001112 • Flip bits: 11100110 • Add 1: 11100110 +00000001 11100111

  4. Your Turn • Assuming an 8-bit restriction, what is -2110 in 2’s complement? • Flip bits • Add 1 Answer: 111010112

  5. Your Turn • Assuming an 8-bit restriction, what is -3010 in 2’s complement? • Flip bits • Add 1 Answer: 111000102

  6. Example ( Neg Pos ) -410 410 11002 01002 • Flip bits: 0011 • Add 1: 0011 +0001 0100

  7. Another Example ( Neg  Pos ) -2910 2910 111000112 000111012 • Flip bits: 00011100 • Add 1: 00011100 +00000001 00011101

  8. Your Turn • Assuming 2’s complement, what is the decimal value of 111110012? • Flip bits • Add 1 Answer: -710

  9. Your Turn • Assuming 2’s complement, what is the decimal value of 111010102? • Flip bits • Add 1 Answer: 22

  10. 2’s Complement Chart

  11. 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

  12. 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

  13. 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

  14. Positive Addition Example 1210 + 410 = 1610 Assuming 2’s complement 000010102 1210 +000000102+ 410 000011002 1610

  15. Negative Addition Example -1210 + -410 = -1610 111101002 -1210 +111111002+ -410 111100002 -1610 NOTE: We ignored the last overflow bit on the left!

  16. Your Turn • Show the binary addition of -14 + -3 = -17

  17. Subtraction Example 1610 – 410 = 1610 + -410 = 1210 000100002 +111111002 000011002 NOTE: We ignored the last overflow bit on the left!

  18. Your Turn • Show the binary subtraction of 23 – 10 = 13

  19. 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

  20. 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!

  21. 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!

More Related