To start, let’s keep our calculator as simple as possible. We’ll give it just two functions:
- Add two numbers together
- Subtract one number from another
We’ve already covered addition. Now we’ll see if we can adapt our adder to make a subtractor.
subtraction
Subtraction looks similar to addition:
- Starting at the lowest column, we take each pair of digits and subtract one from the other
- If we go below 0, then we carry negatively into the next column
For example, to calculate 147 - 52
, starting with the lowest digits:
A | B | result | carry |
---|---|---|---|
7 | -2 | 5 | |
4 | 5 | 9 | -1 |
1 | 0 | 0 |
Giving the result 95
.
In binary, this would be 10010011 - 110100
:
A | B | result | carry |
---|---|---|---|
1 | 0 | 1 | |
1 | 0 | 1 | |
0 | 1 | 1 | -1 |
0 | 0 | 1 | -1 |
1 | 1 | 1 | -1 |
0 | 1 | 0 | -1 |
0 | 0 | 1 | -1 |
1 | 0 | 0 |
Giving the result 1011111, which is 95
.
representing this
As before, let’s look at how we want each pair of digits to behave:
- 0 – 0 ⇒ 0
- 1 – 0 ⇒ 1
- 0 – 1 ⇒ 1, carry -1
- 1 – 1 ⇒ 0
Ignoring the carry for now, this gives us the truth table . Like with the adder, this is just an XOR gate again. So it looks like we may be able to reuse much of our adder design.
carry
The carry’s truth table is . This doesn’t look familiar…
… but if we invert A, which corresponds to swapping the columns of the table, we get . This is just an AND gate.
So the carry is equivalent to NOT(A) AND(B)
.