3.9. Two's Complement

3.9.1. Format

The most common format used to represent signed integers in modern computers is two's complement.

A positive integer in two's complement always has a 0 in the leftmost bit (sign bit) and is represented the same way as an unsigned binary integer.

	+1410 = 01110two's comp
	

3.9.2. Negation

To negate a number, a process sometimes called taking the two's complement, we invert all the bits and add one.

	-1410 = 10001 + 1
	    = 10010two's comp
	

Note that the same process works for both positive and negative numbers. Subtracting one and inverting produces the same results as inverting and adding 1.

10010 - 1 = 10001, inverted = 01110 10010 inverted = 01101, + 1 = 01110

	-(0001) = 1110 + 1 = 1111
	-(1111) = 0000 + 1 = 0001
	-(1110) = 0001 + 1 = 0010
	-(0010) = 1101 + 1 = 1110
	-(1000) = 0111 + 1 = 1000   Ooops!
	

Convert the following 4-bit 2's comp values to decimal:

	0111 = +(1 + 2 + 4) = +7
	1000 = -(0111 + 1) = -(1000) = -8
	0110 = +(2 + 4) = +6
	1001 = -(0110 + 1) = -0111 = -(1 + 2 + 4) = -7
	1110 = -(0001 + 1) = -0010 = -2
	

3.9.3. Addition and Subtraction

Addition works exactly like unsigned addition. This means a computer that uses 2's complement to store signed integers can use the same adder circuit to do both signed and unsigned addition. Subtraction is done by negating and adding.

	Binary      Unsigned    2's comp
	    0 1
	    0101        5       +5
	+   1001    +   9   +   -7
	--------------------------------
	    1110       14       -2
	

3.9.4. Range

Two's complement essentially takes one bit away from the value for use as a sign bit. Since we have one fewer binary digit, the maximum value is 1/2 what it would be for an unsigned number with the same number of bits.

The largest positive value in N-bit two's complement is 0111...111, which is 2N-1-1.

The smallest negative value in N-bit two's complement is 1000...000, which is -2N-1.

Table 3.2. Two's Complement Integer Ranges

BitsRange
8-270 (-128) to +27-1 (+127)
16-215 (-32,768) to +215-1 (32,767)
32-231 (-2,147,483,648) to +231-1 (+2,147,483,647)
64-263 (-9,223,372,036,854,775,808) to +263-1 (9,223,372,036,854,775,807)

3.9.5. Comparison

Comparison of two's complement values is not the same as unsigned comparison if the signs are different:

	A       B       Unsigned    Two's comp
	0111    0110    >           >
	1111    1000    >           >
	0111    1111    <           >
	

This is due to the fact that two's complement rearranges the binary patterns on the number line, so that the latter half of the patterns (those beginning with 1, are less than the first half).

	Unsigned    0       7       8       15
		    0000    0111    1000    1111
	Two's comp  0       +7      -8      -1      (out of sequence)
	

3.9.6. Overflow Detection

Overflow in two's complement is determined by a result with the wrong sign. I.e., if you add two positives and get a negative result, or add two negatives and get a positive.

It is not possible to get an overflow when adding numbers of oppositve signs!

	    111
	    0111    +7      0111    +7      1111    -1
	+   0011    +3      1000    -8      1000    -8    
	-----------------------------------------------------------------
	    1010    -6      1111    -1      0111    +7
	

3.9.7. Extension and Reduction

Two's complement values are extended to larger formats by copying the sign bit to all new positions:

	4-bit       8-bit       16-bit              Decimal
	0111        00000111    0000000000000111    +7
	1110        11111110    1111111111111110    -2