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.

+14_{10}= 01110_{two's comp}

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

-14_{10}= 10001 + 1 = 10010_{two'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

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

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 2^{N-1}-1.

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

**Table 3.2. Two's Complement Integer Ranges**

Bits | Range |
---|---|

8 | -2^{7}0 (-128) to
+2^{7}-1 (+127) |

16 | -2^{15} (-32,768) to
+2^{15}-1 (32,767) |

32 | -2^{31} (-2,147,483,648) to
+2^{31}-1 (+2,147,483,647) |

64 | -2^{63}
(-9,223,372,036,854,775,808) to
+2^{63}-1
(9,223,372,036,854,775,807) |

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)

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