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

How to build a binary to Gray code converter

A binary to Gray code converter keeps the top bit and XORs every pair of neighbouring bits: for 4 bits, G3 = B3, G2 = B3 XOR B2, G1 = B2 XOR B1 and G0 = B1 XOR B0. Three XOR gates do the whole conversion.

In Gray code consecutive numbers differ in exactly one bit, which is why it is used for rotary encoders and to avoid glitches when a multi-bit value changes. Converting from binary needs no carries at all: each Gray bit just records whether a binary bit differs from the one to its left. Going back the other way is a running XOR from the top (B3 = G3, B2 = B3 XOR G2, and so on), which the Gray code converter tool computes for any number. All 16 four-bit codes are checked 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 binary inputs B3 (most significant) to B0.
  2. The top Gray bit is the top binary bit: wire B3 straight to G3.
  3. For each lower bit, XOR the binary bit with the one above it: G2 = B3 XOR B2, G1 = B2 XOR B1, G0 = B1 XOR B0.
  4. Probe G3 to G0 and count the inputs up from 0000: every step changes exactly one Gray output, even 0111 to 1000, where all four binary bits flip.

4-bit binary to Gray code table

DecimalBinary B3..B0Gray G3..G0
000000000
100010001
200100011
300110010
401000110
501010111
601100101
701110100
810001100
910011101
1010101111
1110111110
1211001010
1311011011
1411101001
1511111000

Read down the Gray column: each row differs from the one above in a single bit, and 15 (1000) differs from 0 (0000) in one bit too, so the code wraps cleanly. In one line the conversion is G = B XOR (B shifted right by 1).

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

Frequently asked

How do you convert binary to Gray code?

Keep the most significant bit, then XOR each pair of adjacent bits moving right: G3 = B3, G2 = B3 XOR B2, G1 = B2 XOR B1, G0 = B1 XOR B0. Equivalently, Gray = binary XOR (binary shifted right by one).

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

Three. The top bit passes straight through, and each of the other three Gray bits is one XOR of two neighbouring binary bits.

How do you convert Gray code back to binary?

Keep the top bit, then XOR each new Gray bit with the binary bit you just produced: B3 = G3, B2 = B3 XOR G2, B1 = B2 XOR G1, B0 = B1 XOR G0. Unlike the forward direction, each bit depends on the one before, so the XORs form a chain.

Why is Gray code used?

Because only one bit changes between consecutive values, a reading taken while the value is changing can be off by at most one step. That makes it the standard code for rotary and linear position encoders, and it is also how Karnaugh maps order their rows and columns.