Signed Number Representations - Two's Complement

Two's Complement

8 bit two's complement
Binary value Two's complement interpretation Unsigned interpretation
00000000 0 0
00000001 1 1
01111110 126 126
01111111 127 127
10000000 −128 128
10000001 −127 129
10000010 −126 130
11111110 −2 254
11111111 −1 255

The problems of multiple representations of 0 and the need for the end-around carry are circumvented by a system called two's complement. In two's complement, negative numbers are represented by the bit pattern which is one greater (in an unsigned sense) than the ones' complement of the positive value.

In two's-complement, there is only one zero (00000000). Negating a number (whether negative or positive) is done by inverting all the bits and then adding 1 to that result. This actually reflects the ring structure on all integers modulo 2N: . Addition of a pair of two's-complement integers is the same as addition of a pair of unsigned numbers (except for detection of overflow, if that is done); the same is true for subtraction and even for N lowest significant bits of a product (value of multiplication). For instance, a two's-complement addition of 127 and −128 gives the same binary bit pattern as an unsigned addition of 127 and 128, as can be seen from the 8 bit two's complement table.

An easier method to get the negation of a number in two's complement is as follows:

Example 1 Example 2
1. Starting from the right, find the first '1' 0101001 0101100
2. Invert all of the bits to the left of that one 1010111 1010100

Method two:

  1. Invert all the bits through the number
  2. Add one

Example: for +1 which is 00000001 in binary:

  1. ~00000001 → 11111110
  2. 11111110 + 1 → 11111111 (−1 in two's complement)

Read more about this topic:  Signed Number Representations

Famous quotes containing the word complement:

    There may be as much nobility in being last as in being first, because the two positions are equally necessary in the world, the one to complement the other.
    José Ortega Y Gasset (1883–1955)

    A healthy man, indeed, is the complement of the seasons, and in winter, summer is in his heart.
    Henry David Thoreau (1817–1862)