Binary to Gray Code Conversion Explained

In digital systems, the way we encode numbers matters as much as the numbers themselves. Binary to Gray code conversion is one of the most elegant encoding transformations in digital design, built on a single property: when you count up through Gray code, only one bit changes at each step. This single-bit-change behaviour makes Gray code indispensable in rotary encoders, Karnaugh maps, finite state machines, and clock-domain crossing circuits. This reference explains what Gray code is, why only one bit changes, how to convert between binary and Gray code using the XOR method, and how to build a converter from XOR gates.

What Is Gray Code (Reflected Binary Code)?

Gray code, also called reflected binary code (RBC), is a non-weighted binary numeral system in which two successive values differ in only one bit position. It was patented by Frank Gray of Bell Labs in 1953, originally to reduce errors in electromechanical switching systems. Unlike standard binary, where each bit carries a fixed positional weight (1, 2, 4, 8, …), Gray code bits have no such weighting, so you cannot read a Gray value’s magnitude by simple place-value arithmetic. Its value lies entirely in the single-bit transition property.

The name “reflected” describes how the code is constructed. An n-bit Gray code sequence can be built by taking the (n-1)-bit sequence, reflecting it (listing it in reverse), prefixing 0 to the original half and 1 to the reflected half. This recursive mirror construction guarantees that adjacent entries—including the wrap-around from the last value back to the first—differ by exactly one bit, which is why Gray code is also described as a cyclic code.

Why Only One Bit Changes

Consider ordinary binary counting from 3 (0011) to 4 (0100): three bits flip simultaneously. In real hardware, bits never switch at precisely the same instant. During the transition the output can momentarily pass through invalid intermediate states such as 0111 or 0000, producing transient glitches. If a sensor or counter is sampled at that moment, it may read a wildly wrong value.

Gray code eliminates this hazard. Because only one bit changes between consecutive values, there is no ambiguous intermediate state to misread. The worst case is that the output is read either one step early or one step late—never a large erroneous jump. This is the fundamental reason Gray code is preferred wherever a mechanical or asynchronous value is converted into digital form.

Binary to Gray Code Conversion: The XOR Method

The conversion from binary to Gray code uses the exclusive-OR (XOR) operation and is remarkably simple. For an n-bit binary number with bits Bn-1 (MSB) down to B0 (LSB), the Gray code bits Gn-1 … G0 are produced by these rules:

  • The most significant bit is copied unchanged: Gn-1 = Bn-1
  • Every other Gray bit is the XOR of the current binary bit and the binary bit immediately to its left (the next higher bit): Gi = Bi+1 ⊕ Bi

Equivalently, in a single machine-level expression, the whole conversion is G = B ⊕ (B >> 1): XOR the binary number with a copy of itself shifted right by one position.

XOR Truth Table

Since the entire algorithm rests on XOR, it helps to recall its behaviour. XOR outputs 1 only when its two inputs differ:

ABA ⊕ B
000
011
101
110

Worked Example: Converting 1011 to Gray Code

Take the 4-bit binary number B = 1011 (decimal 11), with B3=1, B2=0, B1=1, B0=1. Apply the rules bit by bit:

  1. G3 = B3 = 1 (MSB copied down)
  2. G2 = B3 ⊕ B2 = 1 ⊕ 0 = 1
  3. G1 = B2 ⊕ B1 = 0 ⊕ 1 = 1
  4. G0 = B1 ⊕ B0 = 1 ⊕ 1 = 0

The Gray code is therefore 1110. The cross-check using the shift method gives the same result: 1011 ⊕ 0101 = 1110.

Complete 4-Bit Binary to Gray Code Table

The following table lists every 4-bit value, its standard binary representation, and its Gray code equivalent. Reading the Gray column top to bottom, confirm that each row differs from the one above it in exactly one bit—including the wrap from 1111 (Gray 1000) back to 0000.

DecimalBinary (B3B2B1B0)Gray (G3G2G1G0)
000000000
100010001
200100011
300110010
401000110
501010111
601100101
701110100
810001100
910011101
1010101111
1110111110
1211001010
1311011011
1411101001
1511111000

Gray to Binary Code Conversion

The reverse conversion—recovering binary from Gray code—also uses XOR, but with a key difference: it is a cumulative (running) XOR rather than a comparison of neighbouring input bits. The rules are:

  • The most significant bit is copied unchanged: Bn-1 = Gn-1
  • Each subsequent binary bit is the XOR of the previously computed binary bit and the current Gray bit: Bi = Bi+1 ⊕ Gi

Notice the asymmetry: binary-to-Gray XORs two input bits, while Gray-to-binary feeds each freshly computed output bit forward into the next stage.

Worked Example: Converting Gray 1110 Back to Binary

Starting from G = 1110 (the value produced earlier):

  1. B3 = G3 = 1
  2. B2 = B3 ⊕ G2 = 1 ⊕ 1 = 0
  3. B1 = B2 ⊕ G1 = 0 ⊕ 1 = 1
  4. B0 = B1 ⊕ G0 = 1 ⊕ 0 = 1

The recovered binary is 1011, confirming that the two conversions are exact inverses of each other.

Binary to Gray Code Converter Circuit

Because every Gray output bit is a simple two-input XOR (except the MSB, which is a direct wire), the hardware for a binary to gray code converter is purely combinational and requires only n-1 XOR gates for an n-bit number. There are no feedback paths and no memory elements, so the circuit settles to its result after a single gate propagation delay.

For a 4-bit converter, the gate-level connections are:

  • G3 = B3 — a straight connection, no gate needed
  • G2 = B3 ⊕ B2 — one XOR gate
  • G1 = B2 ⊕ B1 — one XOR gate
  • G0 = B1 ⊕ B0 — one XOR gate

Three XOR gates, each fed by two adjacent binary inputs, fully implement the transformation. This is an archetypal example of a purely combinational building block; if you want a refresher on how such logic differs from circuits that store state, our guide on what a combinational circuit is and how it works is a useful companion. The Gray-to-binary converter uses the same number of XOR gates but chains them, so its propagation delay grows with word length—a classic area-versus-speed trade-off addressed in optimised designs.

Applications of Gray Code

The single-bit-change property drives every major use of Gray code:

  • Rotary and absolute position encoders: Optical and magnetic shaft encoders print Gray-coded tracks on their discs. As the shaft rotates past a boundary, only one track changes, so the decoded position never jumps erroneously even if the read heads are slightly misaligned.
  • Karnaugh maps (K-maps): The rows and columns of a K-map are labelled in Gray code order precisely so that horizontally or vertically adjacent cells differ in one variable, allowing logic terms to be grouped and minimised visually.
  • Clock-domain crossing (CDC): When a multi-bit counter value—such as a FIFO pointer—must pass between two asynchronous clock domains, it is Gray-encoded first. Because only one bit changes per increment, the synchronising flip-flops can only ever latch the old or the new value, never a corrupt mix of bits.
  • Error reduction in digital communication and state machines: Gray-coded state assignments in finite state machines and Gray-mapped symbols in some digital modulation schemes limit the number of bit errors that a single misread transition can cause.

Gray-coded counters and FIFO pointers are a staple of synchronous RTL design. Writing them correctly—and verifying them—draws on exactly the register-transfer-level skills covered across Verilog and SystemVerilog; if you are comparing those languages, see our overview of Verilog vs SystemVerilog.

Learn Digital Design with ChipXpert

Mastering encodings like Gray code is a stepping stone to building real synchronous circuits, FIFOs, and finite state machines in hardware description languages. If you want structured, hands-on training that takes you from Boolean fundamentals to industry-grade RTL, explore the ChipXpert Advanced RTL Design course, where converters, counters, and clock-domain-crossing techniques are taught with practical lab exercises.

Frequently Asked Questions

What is the formula for binary to Gray code conversion?

The formula is G = B ⊕ (B >> 1). In bit terms, the most significant Gray bit equals the most significant binary bit, and every other Gray bit Gi equals Bi+1 ⊕ Bi—the XOR of each binary bit with the bit immediately to its left.

Why is Gray code called reflected binary code?

Gray code is called reflected binary code because an n-bit sequence is generated by reflecting (reversing) the (n-1)-bit sequence, prefixing 0 to the original half and 1 to the mirrored half. This recursive reflection guarantees that every pair of adjacent codes, including the cyclic wrap-around, differs in exactly one bit.

How do you convert Gray code back to binary?

Copy the most significant Gray bit directly to become the most significant binary bit. Then compute each remaining binary bit as the XOR of the previously computed binary bit and the current Gray bit: Bi = Bi+1 ⊕ Gi. This running XOR exactly inverts the binary-to-Gray transformation.

How many XOR gates does a binary to Gray code converter need?

An n-bit binary to Gray code converter needs n-1 two-input XOR gates. The most significant bit is passed through as a direct wire, and each of the remaining n-1 output bits is produced by one XOR gate fed by two adjacent binary inputs. A 4-bit converter therefore uses three XOR gates.

Where is Gray code used in real digital systems?

Gray code is used in absolute rotary and position encoders, in labelling Karnaugh maps so adjacent cells differ by one variable, and in clock-domain-crossing circuits where multi-bit counter values such as FIFO pointers are Gray-encoded to prevent corrupt readings when sampled by an asynchronous clock.

🎓 Take VLSI Courses Online → Explore ChipXpert Online  –  Live VLSI courses with practical EDA lab work.
Have questions about this topic?
Share your question in comments or talk to our mentor team for batch guidance.

Ask the Admin Team

Drop your basic question in comments: eligibility, prerequisites, tools, fee range, and placement support.

Our team reviews and responds regularly.

Tags :
Share This :