Edexcel International Advanced Level Computer Science, point 2.2.1
Definition and function
Binary addition and subtraction are the basic arithmetic operations the ALU performs on bit patterns. Every other arithmetic operation (multiplication, division, and so on) is built from them.
Binary addition
The rules, for one column:
| Sum | Result bit | Carry |
|---|---|---|
| 0 + 0 | 0 | 0 |
| 0 + 1 | 1 | 0 |
| 1 + 0 | 1 | 0 |
| 1 + 1 | 0 | 1 |
| 1 + 1 + 1 (with a carry in) | 1 | 1 |
Work from the right, carrying into the next column, as in denary.
Unsigned example
in 8 bits:
. ✓ The result fits in 8 bits (the largest unsigned value is 255), so there is no overflow.
Binary subtraction
Method 1: borrowing (unsigned)
Work from the right, borrowing from the next column when you need to take 1 from 0 (a borrowed 1 is worth 2 in the column to its right). In the diagram, a small 1 marks each column that lends a 1.
:
Method 2: add the two's complement (signed and unsigned)
Computers subtract by adding the negative: . Find by inverting and adding 1 (2.1.3), then add. Any carry out of the top bit is discarded.
in 8 bits:
-
00010010. Invert:11101101. Add 1:11101110(). - Add to
00101101:
The carry out of the top bit is discarded, so the result is 00011011 . ✓
This is why the ALU needs only an adder (plus a circuit to invert bits): one circuit does both addition and subtraction.
Signed numbers
With two's complement (2.1.3), signed numbers are added in exactly the same way as unsigned numbers. The sign takes care of itself.
Example: in 8 bits
11110001 . ✓
Example:
The carry out is discarded, and 11001110 . ✓
Checking the result fits
The result must be within the range for the number of bits ( to for 8 bits). If it isn't, there is an overflow (2.2.3).
Worked example
Task: in 8-bit two's complement, work out .
-
00011110. -
01001011. Invert:10110100. Add 1:10110101(). - Add:
11010011. ✓ (.)
Exam tip: line up the columns and write the carries above them: a mark can be given for evidence of adding with carries, even if the final answer is wrong. If a question says to work in 8 bits throughout, pad both numbers to 8 bits first (1s in front of a negative). When subtracting using two's complement, show the invert, add 1 and then the addition, and discard the carry out of the top bit. Always check the answer by converting back to denary, and check that it fits in the number of bits.
Check yourself
What is 00110101 + 00011011?
01010000 (53 + 27 = 80).
How does a computer subtract B from A?
It adds the two's complement of B to A (invert B, add 1, then add), discarding any carry out of the top bit.
Why is it useful that subtraction can be done by addition?
The ALU needs only an adder, plus a circuit to invert bits, so the hardware is simpler.
Work out 12 − 20 in 8-bit two's complement
12 = 00001100 and −20 = 11101100. The sum is 11111000, which is −8.