Edexcel International Advanced Level Computer Science, point 2.1.3
Definition
Two's complement is the method computers use to store signed integers (positive, negative and zero) in binary. The most significant bit (MSB) has a negative place value, and the other bits have the usual positive values.
Function and why needed
- It lets one pattern of bits stand for both positive and negative numbers.
- It has only one representation of zero (unlike sign-and-magnitude, which has +0 and −0).
- Addition and subtraction use the same circuit as for unsigned numbers (2.2.1), so the hardware is simpler.
Characteristics
For an 8-bit two's complement number the place values are:
| −128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|
- If the MSB is 0, the number is zero or positive.
- If the MSB is 1, the number is negative.
- The range for bits is:
| Bits | Range |
|---|---|
| 4 | −8 to +7 |
| 8 | −128 to +127 |
| 16 | −32,768 to +32,767 |
There is one more negative value than positive (for 8 bits: −128 exists, but +128 doesn't).
| Pattern | Value |
|---|---|
| 0111 1111 | +127 (largest) |
| 0000 0001 | +1 |
| 0000 0000 | 0 |
| 1111 1111 | −1 |
| 1000 0000 | −128 (smallest) |
Operation
Reading a two's complement number
Add the place values, using −128 for the MSB.
11010011
Converting a negative denary number to two's complement
To write −45 in 8 bits:
- Write the positive value in binary:
00101101. - Invert every bit (swap 0 and 1):
11010010. - Add 1:
11010011.
Check: . ✓
Converting a negative two's complement number to denary
For 10110100:
- The MSB is 1, so the number is negative.
- Invert:
01001011. - Add 1:
01001100. - So the value is −76.
Check: . ✓
Negation (changing the sign)
The same method changes any number's sign: invert all bits and add 1.
| Start | Invert | Add 1 | Result |
|---|---|---|---|
00000101 (+5) |
11111010 |
11111011 |
−5 |
11111011 (−5) |
00000100 |
00000101 |
+5 |
A quick method for writing a negative number: copy the bits from the right up to and including the first 1, then invert all the bits to its left.
Sign extension (padding to the full width)
A two's complement number can be widened to more bits by copying its sign bit into the new places on the left: 1s for a negative number, 0s for a positive one. Its value doesn't change.
This gives another way to write a negative number in 8 bits: work it out in the fewest bits that will hold it, then pad it with 1s.
−44:
- , so with a 0 sign bit in front it is
0101100(7 bits). - Invert:
1010011. Add 1:1010100. This is −44 in 7 bits. - Pad with 1s on the left to 8 bits:
11010100.
Check: . ✓
Benefits and drawbacks
- Benefits: one zero; arithmetic works with one circuit; easy negation.
- Drawbacks: the range is not symmetrical; the patterns are harder for people to read at a glance; a number needs the width to be known (the same bits mean different things in 8 and 16 bits). Numbers must be sign-extended when widened (see above):
11010011in 8 bits becomes1111111111010011in 16 bits.
Worked example
Task: (a) write −100 in 8-bit two's complement.
(a)
-
01100100. - Invert:
10011011. - Add 1:
10011100.
(b) Find the denary value of 11001001.
- By place values: the MSB is 1, so it is negative: .
- By negation: invert to
00110110, add 1 to get00110111, so the value is −55. ✓
Range check: −100 lies between −128 and +127, so it fits in 8 bits. −200 would not fit, and would need 9 bits or more.
Exam tip: always state the number of bits, and show the steps (positive binary, invert, add 1). Check by adding the place values with −128 for the MSB. Remember the range: to . A common mistake is to forget to add 1 after inverting. A leading 1 means negative and a leading 0 means positive. If a question says to work in 8 bits throughout, pad every value to 8 bits (1s in front of a negative, 0s in front of a positive).
Check yourself
What is the range of 8-bit two's complement numbers?
−128 to +127.
Write −6 in 8-bit two's complement
11111010 (6 is 00000110, invert 11111001, add 1 gives 11111010).
What denary value is 11100001 in 8-bit two's complement?
−128 + 64 + 32 + 1 = −31.
How do you change the sign of a two's complement number?
Invert all the bits and add 1.
Give one advantage of two's complement over sign and magnitude
Only one representation of zero, and addition and subtraction can use the same circuit.