Two's complement

From Citizendium
Revision as of 15:46, 19 March 2008 by imported>Chris Day
Jump to navigation Jump to search
This article is developing and not approved.
Main Article
Discussion
Definition [?]
Related Articles  [?]
Bibliography  [?]
External Links  [?]
Citable Version  [?]
 
This editable Main Article is under development and subject to a disclaimer.

Two's complement is a signed binary numbering system. It is one commonly used in low-level hardware implementations, due to the ease of designing adders and subtracters for it. While sometimes harder for people to understand and work with, it's easier for computers to use than one's complement.

To convert an unsigned binary number into its two's complement negative, one first toggles each of the digits, and then adds one. Positive two's complement numbers are identical to their unsigned equivalents, except that the first digit is a zero. Whereas an unsigned binary number with N digits has a range from 0 to (2^N)-1, a two's complement number spans - 2^(N-1) through 2^(N-1) -1.

Addition and subtraction of two's complement numbers is easy. Since subtraction is essentially the addition of a negative (which is to say that A - B = A + (-B)), reversing the one of the numbers and adding them creates subtraction. To do this in hardware, one uses a control bit (A), which is 0 for addition and 1 for subtraction. This control bit is XORed with each Y input, and serves as the carry-in for the least significant bit. When A=0, the adder functions normally. When A=1, each digit in Y is reversed and 1 is added to it.


Two's complement