TechnologyUK - Number Systems Logo

Two's Complement

Because of the limitations of one's complement when carrying out arithmetic operations such as multiplication and division, the most commonly used way of representing signed integers on computers is to use two's complement, because it allows the logic that handles arithmetic functions to be implemented more easily in hardware. The two's complement of a binary number is the value obtained by subtracting the number from a large power of 2 (specifically, from 2n for an n-bit number). As with one's complement, the most significant bit is used to indicate the sign (0 = positive, 1 = negative), and positive numbers are represented in the same way. To negate a positive number, its two's complement is used. Taking the unsigned number 310 as an example, this would be represented as a positive 8-bit two's complement binary number as 000000112. The value of 28 expressed in standard binary format is 100000000. In order to find the two's complement of +3, therefore, we would carry out the following arithmetic operation:

 100000000
- 00000011
  --------
= 11111101

Negative binary numbers can thus be represented using the two's complement of their absolute value. The absolute value of the largest negative number that can be represented with a given number of bits is always one more than that of the largest positive number that can be represented using the same number of bits. Thus, a two's complement 8-bit binary number can represent signed integer values from −128 to +127 (note that the two's complement of -128 is -128). For similar reasons, zero has only a single representation in a two's complement binary system, since the two's complement of 0 is 0. It is worth noting that the two's complement of a binary number can also be obtained by adding 1 to the one's complement of that number. Putting this in simple terms, you can get the two's complement of a number simply by inverting all the bits (i.e. changing ones to zeros, and zeros to ones) to get the one's complement, and adding 1 to the result (any resulting carry past the most significant bit is ignored). The range of signed 4-bit integers that can be represented using two's complement is shown in the table below.

4-bit Signed Binary Integers
DecimalBinaryDecimalBinary
00000-81000
10001-11111
20010-21110
30011-31101
40100-41100
50101-51011
60110-61010
70111-71001

Like one's complement, two's complement allows an addition operation to be used to carry out both addition and subtraction. To subtract one binary number x from another binary number y using two's complement, simply add y to the two's complement of x (any carry past the most significant bit is simply ignored). The two examples below illustrate the principle.

Example 1: 11810 - 8510 (011101102 – 010101012) = 3310 (001000012)

  01110110
+ 10101011 (two's complement of 01010101)
  --------
= 00100001 (result of standard binary addition, ignoring overflow)

Example 2: 10610 + -7910 (011010102 + 101100002) = 2710 (000110112)

  01101010
+ 10110001 (two's complement of 01001111)
  --------
= 00011011 (result of standard binary addition, ignoring overflow)

Note that although binary arithmetic (addition, subtraction, multiplication and division) is simplified by the use of the two's complement representation of signed binary values, the problem remains that an arithmetic operation involving numbers having a fixed number of bits may well produce a result that requires more than the number of bits provided. The addition of the 8-bit signed integers 11710 and 9610, for example, would produce the result 21310. This result is outside the range of values that can be stored as an 8-bit signed binary integer, regardless of the type of representation used (sign and magnitude, one's complement, or two's complement). For this reason, computers will carry out checks to determine whether the result of an arithmetic operation will result in an overflow, and as a result will often need to promote a value from one data type to another (e.g. from integer to long integer). When converting an 8-bit two's complement number to a 16-bit two's complement number, a process of sign extension occurs, in which the sign bit (the most significant bit of the original 8-bit binary number) is repeated in every bit position to the left of it in the new 16-bit version. The following examples illustrate this:

Example 1:

010101012 (+8510) = 00000000 010101012 (as a 16-bit number)

Example 2:

001110112 (-5910) = 11111111 101010112 (as a 16-bit number)

Finally, it is worth noting that binary multiplication is also relatively straightforward in a two's complement system. For example, a bitwise multiplication of 111101002 (-1210) and 000010002 (+810) gives the result 111101000002. Since we are dealing with 8-bit numbers, we can drop the leftmost three bits, leaving 101000002. Since the most significant bit is 1, we know the result has a negative value. The two's complement of 101000002 is 011000002, or 9610, so the result represents -9610, which is the correct result of multiplying -1210 and +810.