Skip to content
digiwleeaTools
LearnGlossaryAboutCheat sheetHow a CPU worksToolsOpen the lab →

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.

Live circuit: click the inputs to toggle them and watch the outputs update. It runs the real simulator, right here on the page.

Open this circuit in the lab →

What you need

Step by step

  1. Place the digits A3 to A0 and B3 to B0, a borrow-in Bin, and probes D3 to D0 and Bout.
  2. Form the 9's complement of B: C3 = NOR(B3 OR B2, B1), C2 = B2 XOR B1, C1 = B1, C0 = NOT B0.
  3. Add A and C in a BCD adder with carry-in NOT Bin.
  4. Take the adder's digit as D and invert its decimal carry for Bout.
  5. 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

ABBin9 - BA + (9 - B) + NOT BinDBoutReads as
0111 (7)0011 (3)0011014010007 - 3 = 4
0011 (3)0101 (5)0010081000113 - 5 = 8, borrow
1001 (9)1001 (9)0000010000009 - 9 = 0
1000 (8)0010 (2)1011115010108 - 2 - 1 = 5
0101 (5)0101 (5)1010091001115 - 5 - 1 = 9, borrow
0000 (0)1001 (9)1000000000110 - 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

half adderfull adderhalf subtractorfull subtractor2:1 multiplexer4:1 multiplexerXOR gate from NAND gates2-to-4 decoderSR latchD flip-flopJK flip-flopT flip-flop4-bit shift register4-bit ripple carry adderD latch1-to-4 demultiplexer4-bit magnitude comparator4-bit ring counter4-bit Johnson counter4-to-2 priority encoder4-bit synchronous counter3-to-8 decoder8-to-1 multiplexer4-bit carry lookahead adderhalf adder from NAND gates2-bit comparatorBCD to 7-segment decoder4-bit up/down counterdecade counter2-bit multiplier4-bit ALU4-bit array multiplierNOT, AND, OR and NOR from NANDNOT, OR, AND and XOR from NORfull adder from NAND gatesSR latch from NAND gatesparity generator and checker4-bit adder/subtractorBCD adder4-bit universal shift registermod-6 counter4-bit ripple counterbinary to Gray code converterhalf adder from NOR gatesfull adder from NOR gates4-bit register with parallel load1011 sequence detectorbinary to BCD converterD flip-flop from NAND gates1-bit ALUCMOS inverterCMOS NAND gateCMOS NOR gatePISO shift registerSR flip-flop from NAND gatesBCD to excess-3 code converterHamming (7,4) code encoder and correctortraffic light controller4-bit barrel shifterCMOS AND gateCMOS OR gateCMOS XOR gate1-bit magnitude comparatorhalf subtractor from NAND gatesfull subtractor from NAND gates8-to-3 encoderGray to binary converter4-to-16 decoderfull adder using 4:1 multiplexersedge detectorserial adder4-bit carry-select adder4-bit Kogge-Stone adder4-bit carry-skip adder4x4 Wallace tree multiplier8-bit adderfrequency divider (by 2, 4 and 3)switch debouncer (SR latch)3-bit Gray code counterMealy machine (1011 detector)master-slave JK flip-flop (NAND)half subtractor from NOR gates1-to-8 demultiplexerexcess-3 to BCD code convertervending machine FSM4-bit carry-save adder3-bit synchronous counter (JK)CMOS transmission gatetri-state buffer (CMOS)2-to-1 mux from transmission gatesmod-5 counterflip-flop conversion (JK to D)8-bit ALUdecimal to BCD encoderBCD to decimal decoder4-to-1 mux from 2-to-1 muxesXOR gate from NOR gates4-bit LFSR74LS138 decoder

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.