Skip to content
digiwleeaTools
LearnGlossaryAboutCheat sheetHow a CPU worksBuild a CPUToolsOpen the lab →

BCD to Gray code converter: K-map design, live

A BCD to Gray code converter turns the ten BCD digits 0 to 9 into 4-bit Gray code. Using the six unused codes 1010 to 1111 as don't-cares, the K-maps give G3 = B3, G2 = B3 + B2, G1 = B2 XOR B1 and G0 = B1 XOR B0: one OR gate and two XOR gates.

BCD only ever uses the codes 0000 to 1001, so a BCD input never has B3 and B2 (or B3 and B1) both 1. That freedom is what the design uses. A plain binary to Gray converter needs G2 = B3 XOR B2, but on every valid BCD input B3 = 1 only for 8 and 9, where B2 = 0, so B3 XOR B2 and B3 OR B2 always agree, and the K-map with don't-cares picks the simpler OR. G1 and G0 have no don't-cares that help and stay XORs. On the six invalid inputs this circuit's outputs are not Gray code, which is allowed because BCD never produces them. The Gray code converter tool converts any number, and the K-map solver takes don't-cares if you want to repeat the simplification.

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 →

Embed or cite this circuit

Free to embed on a course page, blog or worksheet: paste this code. The circuit stays live, and the small digiwleea badge links back here.

To cite it: digiwleea, “BCD to Gray code converter”, https://digiwleea.wleeaf.dev/tools/build/bcd-to-gray/

What you need

Step by step

  1. Place the BCD inputs B3 (most significant) to B0.
  2. Wire B3 straight to G3.
  3. OR B3 with B2 for G2: on valid BCD inputs this equals B3 XOR B2.
  4. XOR neighbouring bits for the rest: G1 = B2 XOR B1, G0 = B1 XOR B0.
  5. Step the input through 0000 to 1001: each Gray output differs from the previous one in a single bit.

BCD to Gray code truth table

DecimalBCD B3..B0Gray G3..G0
000000000
100010001
200100011
300110010
401000110
501010111
601100101
701110100
810001100
910011101
10 to 151010 to 1111don't care

The ten valid rows are checked on the live circuit before the page is built. The don't-care rows are the reason G2 can be an OR gate instead of an XOR. Built from four 8-to-1 multiplexers instead: BCD to Gray code converter using 8:1 muxes.

You can also read the full theory in the lesson on Gray code. 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.

More builds: Selecting, routing and decoding

All 246 build guides, by topic

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 to Gray code converter in the lab →

Frequently asked

How do you design a BCD to Gray code converter?

Write the truth table for the ten BCD digits, mark the codes 1010 to 1111 as don't-cares, and simplify each output with a K-map. The result is G3 = B3, G2 = B3 + B2, G1 = B2 XOR B1, G0 = B1 XOR B0.

Why is G2 an OR gate and not an XOR?

Because B3 and B2 are never both 1 in a valid BCD digit. For every input that can occur, B3 OR B2 and B3 XOR B2 give the same answer, and the don't-cares let the K-map choose the cheaper OR.

Is BCD to Gray the same as binary to Gray?

For the digits 0 to 9 the outputs are identical, because BCD digits are the binary numbers 0 to 9. The circuits differ only because the BCD version may use the unused codes as don't-cares.

What is 9 in Gray code?

1101. In BCD, 9 is 1001, and the Gray code keeps the top bit and XORs neighbours: 1, 1 XOR 0 = 1, 0 XOR 0 = 0, 0 XOR 1 = 1.