OCR H446, section 1.4.1 (d)
Computers add and subtract binary numbers using exactly the same ideas as column addition in denary, but with only two digits.
Adding binary numbers
The rules for adding single bits are:
| Sum | Result |
|---|---|
| 0 | |
| 1 | |
| 10 (write 0, carry 1) | |
| 11 (write 1, carry 1) |
Work from the right, adding each column, and carrying to the next.
Add (53 + 27).
carries: 1 1 1 1 1 1
0 0 1 1 0 1 0 1 (53)
+ 0 0 0 1 1 0 1 1 (27)
-----------------
0 1 0 1 0 0 0 0 (80)
Right to left:
| Column | Bits | Plus carry | Result bit | New carry |
|---|---|---|---|---|
| 1 | 0 | 0 | 1 | |
| 2 | 1 | 0 | 1 | |
| 3 | 1 | 0 | 1 | |
| 4 | 1 | 0 | 1 | |
| 5 | 1 | 1 | 1 | |
| 6 | 1 | 0 | 1 | |
| 7 | 1 | 1 | 0 | |
| 8 | 0 | 0 | 0 |
The answer is . ✓ (.)
Overflow
If the answer needs more bits than are available, the extra carry is lost, and the stored answer is wrong. This is overflow.
Unsigned example (8 bits): .
11001000 (200)
+ 01100100 (100)
----------
100101100 <- 9 bits needed
Only the right-hand 8 bits are kept: . The true answer is 300, which is more than the largest 8-bit value, 255, so overflow has occurred.
Two's complement example (8 bits): , but the largest value is 127.
01100100 (+100)
+ 01100100 (+100)
----------
11001000 (this reads as -56)
Two positive numbers have given a negative result, which is a sign of overflow. (The sum of two numbers with the same sign has given a result with the opposite sign.)
Subtracting binary numbers
A computer subtracts by adding the negative. To calculate , find in two's complement, then add it to :
Then ignore any carry out of the leftmost bit.
Calculate in 8 bits.
- .
- Invert: . Add 1: . This is .
- Add to 45 ():
00101101 (45)
+ 11101110 (-18)
----------
100011011
- Ignore the carry out of the 8th bit, leaving . ✓ (.)
Calculate .
- in two's complement is .
- .
- This has a leading 1, so it is negative. Its value is . ✓
Because the same adder does both jobs, the processor needs only one circuit for addition and subtraction.
Subtracting directly (borrowing)
In the exam you can also subtract column by column, as in denary, borrowing from the next column when the top bit is too small. Any correct method gets the marks, as long as the binary working is shown.
| Top − bottom − borrow in | Result bit | Borrow from the next column? |
|---|---|---|
| , , | 0 | No |
| 1 | No | |
| , , | 1 | Yes |
| 0 | Yes |
Calculate (178 − 27).
borrows: 1 1 1 1 1
1 0 1 1 0 0 1 0 (178)
- 0 0 0 1 1 0 1 1 (27)
-----------------
1 0 0 1 0 1 1 1 (151)
Right to left:
| Column | Top − bottom | Borrow in | Result bit | Borrow from next |
|---|---|---|---|---|
| 1 | 0 | 1 | Yes | |
| 2 | 1 | 1 | Yes | |
| 3 | 1 | 1 | Yes | |
| 4 | 1 | 0 | Yes | |
| 5 | 1 | 1 | Yes | |
| 6 | 1 | 0 | No | |
| 7 | 0 | 0 | No | |
| 8 | 0 | 1 | No |
The answer is . ✓
Checking your answers
- Convert the operands and the answer to denary and check the sum.
- Check the sign of a two's complement result.
- Check for overflow: did the answer fit in the number of bits?
Worked example
Task: add in 8 bits, and decide whether the result is correct for (a) unsigned numbers, (b) two's complement.
01101100 (108)
+ 00110111 (55)
----------
10100011
- (a) Unsigned: , which equals . ✓ It fits within 255, so there is no overflow.
- (b) Two's complement: the answer has a leading 1, so it reads as . But , which is more than 127, so this is overflow, and the answer is wrong.
The same bit pattern is correct in one interpretation and an error in the other.
Exam tip: show the carries and line up the columns. For subtraction, write the two's complement of the number first, then add. Say whether overflow has happened: for unsigned, the answer needs an extra bit, and for two's complement, the sign of the result is wrong for the numbers added. If an addition overflows, show the extra 1, then show it being dropped to leave 8 bits. Always show the binary working: converting to denary, calculating and converting back scores nothing.
Check yourself
Add
(6 + 3 = 9).
What is overflow?
When the result of a calculation is too large for the number of bits available, so the extra bit is lost and the stored result is wrong.
How does a computer calculate ?
It adds the two's complement of 5 (that is, ) to 12, and ignores any carry out of the leftmost bit.
How can you spot overflow when adding two positive two's complement numbers?
The result has a leading 1, so it appears negative.