AQA 7517, section 4.5.4.3
Signed binary can represent negative integers as well as positive ones. There are several possible schemes, but the one you need to know is two's complement. It is the only representation of negative integers that will be examined.
How two's complement works
In two's complement the leftmost bit has a negative place value. In 8 bits:
| 64 | 32 | 16 | 8 | 4 | 2 | 1 | |
|---|---|---|---|---|---|---|---|
| 1 | 1 | 0 | 1 | 0 | 0 | 1 | 1 |
This is .
- If the leftmost bit is 0, the number is positive (or zero), and you read it as normal.
- If the leftmost bit is 1, the number is negative.
Converting signed binary to decimal
Use the place values, with the leftmost bit counting as a negative value.
Writing a negative number
- Write the positive number in binary.
- Invert every bit (swap 0s and 1s).
- Add 1.
Write in 8-bit two's complement.
- Invert:
- Add 1:
Check: . ✓
Write in 8-bit two's complement. . Invert to get , then add 1 to get . Check: . ✓
The same method turns a negative number back into a positive one: invert and add 1.
A quick alternative: starting from the right, copy bits up to and including the first 1, then invert all the rest.
Range
With bits, two's complement can store
| Bits () | Smallest | Largest |
|---|---|---|
| 4 | 7 | |
| 8 | 127 | |
| 16 | 32 767 |
There are patterns in total. Half have a leading 0 (zero and the positive numbers, up to ) and half have a leading 1 (the negatives). There is only one zero, 00000000, and there is one more negative number than positive.
Subtraction
A computer subtracts by adding the negative: . So it needs only an adder, and the subtraction is done by negating first.
Calculate in 8 bits. Use .
- , so
The carry out of the leftmost bit is discarded, leaving . ✓
Calculate . , where :
The result . ✓ There's no carry out this time, and the leading 1 shows the answer is negative.
Worked example
Task: (a) write in 8-bit two's complement. (b) What is as a decimal number? (c) What is the range of 6-bit two's complement?
- (a) . Invert: . Add 1: . Check: . ✓
- (b) .
- (c) to , which is to .
Exam tip: always state how many bits you are using. An answer in two's complement is meaningless without the word length. Show the invert-and-add-1 working, and check your answer by converting it back.
Check yourself
Convert (two's complement) to decimal
.
Write in 8-bit two's complement
. Invert: . Add 1: . So is .
What is the range of 10-bit two's complement?
to , which is to .
Why does a computer subtract by adding a negative number?
It then needs only one circuit, an adder, for both addition and subtraction.