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

How to build a Gray to binary converter (4-bit)

A Gray to binary converter turns a 4-bit Gray code G3 G2 G1 G0 back into ordinary binary B3 B2 B1 B0 with three XOR gates: the top bit is copied, B3 = G3, and every lower bit is the XOR of the binary bit above it and its own Gray bit.

The binary to Gray converter XORs neighbouring binary bits, so its three XORs work side by side. Going back is a chain: B2 = G3 XOR G2, then B1 = B2 XOR G1 needs B2, and B0 = B1 XOR G0 needs B1, so the XORs ripple from the top bit down. Unrolled, each binary bit is the XOR of every Gray bit from its own position upward; B0 = G3 XOR G2 XOR G1 XOR G0 is the parity of the whole code. Gray code changes exactly one bit per step, which is why position sensors such as rotary encoders output it and why K-map rows and columns are labelled in Gray order; you convert back to binary as soon as you need to do arithmetic. All 16 codes are checked before the page is built. Convert numbers by hand with the Gray code converter.

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 G3 to G0 and probes B3 to B0.
  2. Wire G3 straight to B3.
  3. B2 = G3 XOR G2.
  4. B1 = B2 XOR G1, using the B2 you just made, then B0 = B1 XOR G0.
  5. Step through the Gray sequence 0000, 0001, 0011, 0010, 0110: the binary outputs count 0, 1, 2, 3, 4.

4-bit Gray to binary table

Gray G3 G2 G1 G0Binary B3 B2 B1 B0Decimal
000000000
000100011
001100102
001000113
011001004
011101015
010101106
010001117
110010008
110110019
1111101010
1110101111
1010110012
1011110113
1001111014
1000111115

Reading down the table, each Gray code differs from the one before it in exactly one bit, while the binary column can change several bits at once: 0111 to 1000 changes all four.

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.

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

Frequently asked

How do you convert Gray code to binary?

Copy the most significant bit, then XOR each binary bit you have just found with the next Gray bit: B3 = G3, B2 = B3 XOR G2, B1 = B2 XOR G1, B0 = B1 XOR G0.

How many XOR gates does a 4-bit Gray to binary converter need?

Three. An n-bit converter needs n - 1, the same as the binary to Gray direction, but chained one after another instead of side by side.

What is Gray code 1011 in binary?

B3 = 1, B2 = 1 XOR 0 = 1, B1 = 1 XOR 1 = 0, B0 = 0 XOR 1 = 1, so Gray 1011 is binary 1101, which is 13.

Why is Gray code used?

Consecutive values differ in only one bit, so a sensor reading a position, such as a rotary encoder, never sees a wildly wrong value while its bits change. K-map rows and columns use Gray order for the same reason: neighbouring cells differ in one variable.