How to build a BCD subtractor from logic gates
A BCD subtractor subtracts one decimal digit from another, both stored in BCD (0000 to 1001), with a borrow in and a borrow out. This one adds A to the 9's complement of B in a BCD adder: A - B - Bin = A + (9 - B) + (1 - Bin) - 10, so the adder's digit is the answer and its decimal carry means "no borrow".
Subtracting by adding a complement works in decimal just as two's complement does in binary. The 9's complement of a digit, 9 - B, needs no arithmetic at all, only four small gate equations: C3 = NOT (B3 OR B2 OR B1), C2 = B2 XOR B1, C1 = B1 and C0 = NOT B0. Adding 1 more turns it into the 10's complement, and that 1 comes from the carry-in, which is why the carry-in is NOT Bin. If A - B - Bin is 0 or more, the sum A + (9 - B) + (1 - Bin) is 10 or more, so the BCD adder carries and its digit is exactly A - B - Bin. If A - B - Bin is negative, the sum stays below 10, there is no carry, and the digit is 10 + A - B - Bin: the borrowed digit, just as in subtraction by hand. Either way, Bout = NOT carry. Every pair of BCD digits with both borrow-in values (200 cases) is checked against real decimal subtraction before the page is built.
Open this circuit in the lab →
What you need
- 1× OR + 1 NOR: C3 = NOR(B3 OR B2, B1), the top bit of 9 - B
- 1× XOR: C2 = B2 XOR B1 (C1 = B1 needs no gate)
- 1× NOT: C0 = NOT B0
- 1× NOT: the adder's carry-in, NOT Bin
- 1× BCD adder: A + (9 - B) + NOT Bin, giving the digit D and a decimal carry
- 1× NOT: Bout = NOT carry
Step by step
- Place the digits
A3toA0andB3toB0, a borrow-inBin, and probesD3toD0andBout. - Form the 9's complement of B:
C3 = NOR(B3 OR B2, B1),C2 = B2 XOR B1,C1 = B1,C0 = NOT B0. - Add A and C in a BCD adder with carry-in
NOT Bin. - Take the adder's digit as
Dand invert its decimal carry forBout. - Try 7 - 3: the complement of 3 is 6, and 7 + 6 + 1 = 14, so D = 0100 (4) and Bout = 0. Then 3 - 5: 3 + 4 + 1 = 8, no carry, so D = 1000 (8) and Bout = 1, which reads 13 - 5 = 8 with a borrow.
Examples
| A | B | Bin | 9 - B | A + (9 - B) + NOT Bin | D | Bout | Reads as |
|---|---|---|---|---|---|---|---|
| 0111 (7) | 0011 (3) | 0 | 0110 | 14 | 0100 | 0 | 7 - 3 = 4 |
| 0011 (3) | 0101 (5) | 0 | 0100 | 8 | 1000 | 1 | 13 - 5 = 8, borrow |
| 1001 (9) | 1001 (9) | 0 | 0000 | 10 | 0000 | 0 | 9 - 9 = 0 |
| 1000 (8) | 0010 (2) | 1 | 0111 | 15 | 0101 | 0 | 8 - 2 - 1 = 5 |
| 0101 (5) | 0101 (5) | 1 | 0100 | 9 | 1001 | 1 | 15 - 5 - 1 = 9, borrow |
| 0000 (0) | 1001 (9) | 1 | 0000 | 0 | 0000 | 1 | 10 - 9 - 1 = 0, borrow |
To subtract multi-digit decimal numbers, chain one stage per digit, the least significant first, with each Bout feeding the next stage's Bin. A borrow out of the top digit means the result is negative and the digits hold its 10's complement, the decimal counterpart of a negative two's complement number. Inputs above 1001 are not BCD digits and are not part of the specification.
You can also read the full theory in the lesson on binary-coded decimal. Prefer to build it yourself from scratch? Open the digiwleea lab and place the gates on the canvas, or start from the logic gate simulator.
Build more parts from gates
Each of these is one step from a transistor to a working CPU. Build every gate from CMOS transistors and work all the way up in the free digiwleea lab, with the free theory reference alongside.
Open the BCD subtractor in the lab →Frequently asked
How do you subtract BCD numbers?
Digit by digit, with a borrow between digits. A common circuit adds A to the 9's complement of B in a BCD adder, with carry-in 1 when there is no incoming borrow: the adder's digit is the result digit and its carry means "no borrow".
What is the 9's complement of a BCD digit?
9 minus the digit: 0 becomes 9, 3 becomes 6, 9 becomes 0. In gates it is C3 = NOT (B3 OR B2 OR B1), C2 = B2 XOR B1, C1 = B1 and C0 = NOT B0, valid for the digits 0 to 9.
Why is the borrow the inverse of the BCD adder's carry?
Because the circuit computes A - B - Bin + 10. When A - B - Bin is 0 or more, that sum is at least 10 and the adder carries; when it is negative, the sum is below 10 and it does not. So a carry means no borrow, and Bout = NOT carry.
What is the difference between 9's complement and 10's complement subtraction?
The 10's complement is the 9's complement plus 1. Adding the 10's complement subtracts directly; with the 9's complement alone you add the 1 separately, which is the carry-in here, or as an end-around carry in the older 9's complement method.