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

Decimal to BCD encoder: 10 inputs to 4 bits, live

A decimal to BCD encoder turns one active input out of ten, D0 to D9 (a key on a keypad, say), into the 4-bit BCD code of that digit, B3 B2 B1 B0. Each output ORs the inputs whose digit has that bit set: B0 = D1 + D3 + D5 + D7 + D9, B1 = D2 + D3 + D6 + D7, B2 = D4 + D5 + D6 + D7 and B3 = D8 + D9.

It is the decimal cousin of the 8-to-3 encoder, and the reverse of the BCD to decimal decoder. D0 is not wired to anything, because its code is 0000, which is also what the outputs show when no key is pressed; a practical keypad encoder adds a valid output to tell the two apart. Like every plain encoder, it assumes one input at a time: press 3 and 5 together and it outputs 0111, a 7. Chips such as the 74147 are priority encoders instead, where the highest pressed key wins (see the priority encoder). All 1,024 input combinations are checked against the OR equations 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 inputs D9 to D0 and probes B3 to B0.
  2. For each output bit, list the digits 1 to 9 whose BCD code has that bit set.
  3. OR those inputs together: five for B0, four each for B1 and B2, two for B3. D0 needs no gate.
  4. Turn on one input at a time: the outputs show its digit in BCD.

Decimal to BCD encoder truth table

Active inputB3B2B1B0
D00000
D10001
D20010
D30011
D40100
D50101
D60110
D70111
D81000
D91001

The table assumes exactly one input is 1. With more than one, each output is the OR of their codes, which is why real keypads use a priority encoder.

You can also read the full theory in the lesson on BCD. 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 adderMealy 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 ALUBCD to decimal decoder4-to-1 mux from 2-to-1 muxesXOR gate from NOR 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 decimal to BCD encoder in the lab →

Frequently asked

What is a decimal to BCD encoder?

A circuit with ten inputs, one per decimal digit, and four outputs that give the BCD code of the active input. It is also called a 10-to-4 line encoder.

How many OR gates does a decimal to BCD encoder need?

Four OR gates with up to five inputs: one per output bit. With 2-input gates it takes eleven, as here.

What is the 74147?

A 10-line to 4-line priority encoder chip (TI lists its SN74147 as obsolete). It has nine active-low data inputs, with 0 implied when none is active, and encodes only the highest-order active line. This page builds the simpler non-priority version.

Why is D0 not connected?

Its code is 0000, so it never needs to raise an output. The catch is that 0000 also appears when no input is active, which is why practical encoders add a valid output.