From WikiEducator
Jump to: navigation, search

Two's Complement

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. 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). 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 above table.

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

  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


Two's Complement