A computer stores numbers as bits. There is no minus sign in hardware.
Signed-magnitude uses the leftmost bit as the sign and the rest as the value, but it has
two zeros (0000 and 1000) and needs separate circuits for
addition and subtraction. Two's complement avoids both.
Two wheels below. Left is 1-digit decimal (ten's complement). Right is 4-bit binary (two's complement). Same idea, different bases.
Both wheels start at 4 (marked with the green ring) and compute 4 − 3 = 1. On each wheel, the two buttons take different paths but land at the same place.
4
Start at 4.
0100
Start at 4.
10 − 3. On the binary wheel, 1101 (13)
is 16 − 3. Those are the ten's and two's complements of 3. Adding a complement
gives the same result as subtracting the original, so hardware can drop subtraction
circuits entirely and always add.
For k bits:
1101.1010.
For large k, computing 2k − I is slow. The mechanical version:
0101. Flip to 1010. Add 1 to get 1011.1011. ✓
1100?0011, add 1 to get 0100 = 4.1100 = −4.
With k bits:
4 bits: −8 to +7. 8 bits: −128 to +127. 32 bits: about ±2.1 billion.
| Binary | Signed value |
|---|
In 4 bits, 1000 is −8, but there is no +8 (the largest positive is +7).
Applying the shortcut to 1000: flip to 0111, add 1, back to
1000. Negating the most negative value returns itself.
All four use 4-bit two's complement. Binary answers are exactly 4 digits.
1. Write −5 in 4-bit two's complement.
2. The pattern 1010 is which decimal value?
3. Compute 6 + (−4) in 4-bit two's complement.
4. Compute 0111 + 0001 in 4 bits.
k bits: −2k−1 to 2k−1 − 1.