Two's complement

From Computer History Wiki
Jump to: navigation, search

Two's complement is the now-universal system for representing integers (both positive and negative) in binary; if the high bit is '1', the number is negative; if '0', the remaining bits encode either 0 (if all '0'), or a positive number.

Negative numbers are formed by taking the positive number, complementing it (bit-by-bit inversion), and adding one to the result. So. for example, using a 4-bit word, 1 is '0001'; to get -1, invert, which gives '1110', and add one, giving '1111'.

Note that when considering the possible values available with an N-bit word, since 0 is 'taken' from the 'space' allocated to positive numbers, there is always one more possible negative number than positive. I.e. with a N-bit number, there are 2N-1 negative values, 0, and 2N-1-1 positive values. This means that there is a negative number whose positive counterpart cannot be expressed.


The predecessor to two's complement was one's complement, in which the negative is simply the complement, without the 'add one' step.

Two's complement is preferred because i) the same hardware can be used for unsigned (considering the N bits of a word as representing a number in the range 0 to 2N-1) and two's complement arithmetic, and ii) one's complement has a 'negative zero' (all 1's) as well the other zero, which makes programs more complex.