1 / 14

Two's Complement: Patterns for Representing Negative Numbers

This article explains the patterns and advantages of using two's complement in representing negative numbers in binary. It includes examples, calculations, and overflow detection.

egood
Download Presentation

Two's Complement: Patterns for Representing Negative Numbers

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. Patterns that Add Up to Zero We derive the patterns for negative numbers (instead of direct assignment). Minus one added to plus one gives zero. So if a particular bit pattern results in the pattern that represents zero when added to one, it can represent minus one. 1 111 0001 = 110 1111 = -110 --------- ----- 0000 = 010

  2. Two's Complement 1 110 0010 = 210 1110 = -210 --------- ----- 0000 = 010 1 111 0001 = 110 1111 = -110 --------- ----- 0000 = 010 For every bit pattern of N bits there is a corresponding bit pattern of N bits which produces an N-bit zero when the two patterns are used as operands for the binary addition algorithm. Negative = 2N – Positive

  3. Examples Is arithmetic correct with our encoding ?

  4. Construct the Negative of an Integer in Two's Complement To calculate the N-bit repn of the negative integer: • Reflect (invert) each bit of the bit pattern (change 0 to 1, and 1 to 0). • Add one.

  5. Construct the Negative When each bit of a pattern is reflected then the two patterns added together make all 1's. This works for all patterns: 0110 1010 pattern 1001 0101 pattern reflected --------- 1111 1111 all columns filled Adding one to this pattern creates a pattern of all zero's: 11111 111 row of carry bits 1111 1111 all columns filled 0000 0001 one --------- 0000 0000 But instead, the one is added to the "pattern reflected”. pattern + pattern reflected = all ones pattern + pattern reflected + one = all zeros pattern + (pattern reflected + one ) = all zeros (two's complement positive of pattern) + (two's complement negative of pattern) = all zeros

  6. Range of Integers with 2's Complement Using the algorithm with the repn for zero results in the repn for zero. This is good. Usually "minus zero" is regarded as the same thing as "zero".

  7. Range of Integers with 2's Complement

  8. Twice negation and The "Sign Bit" • With N-bit two's comp repn, the high order bit is • "0" for positive integersand • "1" for negative integers. This is a fortunate result. • The high order bit is sometimes called the sign bit. But it is not really a sign (it does not play a separate role from the other bits). • It takes part in the "binary addition algorithm" just as any bit. The algorithm that creates the repn of the negative of an integer works with both positive and negative integers. Start with N and complement it (make its negative): you get -N. Now complement -N and you get the original N.

  9. Convert two's complement representation into decimal • If the integer is negative, complement it to get a positive integer. • Convert (positive) integer to decimal (as usual). • If the integer was originally negative, put "-" in front of the decimal. The number repd by 1001 is negative (since the "sign bit" is one), so complement: 1001 → 0110 + 1 → 0111 • Convert the result to decimal:  0111 = 710. • Add a negative sign:   -710. • So (in 4-bit two's comp. repn) 1001 represents -710.

  10. Overflow Detection in 2's Complement

  11. Overflow Detection in 2's Complement Carry In == Carry Out • The result is correct if the carry INTO the high order column equals the carry OUT OF the high order column. • The carry bits can both be zero or both be one.

  12. Examples 1000 0100 1000 0100

  13. Subtraction

  14. Two’s complement advantages • Only one Zero pattern • No any sign or magnitude checking. • Calculations are very simple. We are getting quite a bit of use out of the "binary addition algorithm“: • Addintegers represented in unsigned binary. • Add integers represented in two's complement binary. • Subtract integers represented in two's complement binary.

More Related