
Gray codes, also known as reflected binary codes, offer a clever way to minimize errors when digital signals transition between states. By ensuring that only one bit changes at a time, they simplify hardware design and reduce ambiguity in applications ranging from rotary encoders to error correction.
This article quickly revisits the fundamentals of Gray codes and highlights a few practical hints engineers can apply when working with them in real-world circuits.
Understanding reflected binary (Gray) code
The reflected binary code (RBC), more commonly known as Gray code after its inventor Frank Gray, is a systematic ordering of binary numbers designed in a way that each successive value differs from the previous one in only a single bit. This property makes Gray code distinct from conventional binary sequences, where multiple bits may flip simultaneously during transitions.
To illustrate, consider the decimal values 1 and 2. In standard binary, they are represented as 001 and 010, requiring two bits to change when moving from one to the other. In Gray code, however, the same values are expressed as 001 and 011, ensuring that only one bit changes during the increment. This seemingly small adjustment has significant practical implications: it reduces ambiguity and minimizes the risk of misinterpretation during state changes.
Gray codes have long been valued in engineering practice. They help suppress spurious outputs in electromechanical switches, where simultaneous bit changes could otherwise produce transient errors. In modern applications, Gray coding also supports error reduction in digital communication systems. By simplifying logic operations and constraining transitions to a single bit, Gray codes provide a robust foundation for reliable digital design.
Gray code vs. natural binary
Unlike standard binary encoding, where multiple bits may change during a numerical increment, Gray code ensures that only a single bit flips between successive values. This one-bit transition minimizes ambiguity during state changes and enables simple error detection: if more than one bit changes unexpectedly, the system can flag the data as invalid.
This property is especially useful in position encoders and digital control systems, where transient errors from simultaneous bit changes can lead to misinterpretation. The figure below compares the progression of values in natural binary and Gray code, highlighting how Gray code preserves single-bit transitions across the sequence.

Figure 1 Table compares Gray code and natural binary sequences, highlighting single-bit transitions between increments. Source: Author
When it comes to reliability in absolute encoder outputs, Gray code is the preferred choice because it prevents data errors that can arise with natural binary during state transitions. In natural binary, a sluggish system response may momentarily misinterpret a change; for instance, the transition from 0011 to 0100 could briefly appear as 0111 if multiple bits switch simultaneously. Gray code avoids this issue by ensuring that only one bit changes at a time, making the output stream inherently more reliable and easier for controllers to validate in practice.
Furthermore, converting Gray code back to natural binary is straightforward and can be done quickly on paper. Begin by writing down the Gray code sequence and copying the leftmost bit directly beneath it. Then, add this copied bit to the next Gray code bit to the right, ignoring any carries, and place the result beside the first copied digit. Continue this process step by step across the sequence until all bits have been converted. The final row represents the equivalent natural binary value.
For example, consider the Gray code 1011.
- Copy the leftmost bit → binary begins as 1.
- Next, add (XOR) the copied bit with the next Gray bit: 1 XOR 0 = 1 → binary becomes 11.
- Continue: 1 XOR 1 = 0 → binary becomes 110.
- Finally: 0 XOR 1 = 1 → binary becomes 1101.
Thus, the Gray code 1011 corresponds to the natural binary value 1101.
Gray code 1011 is not a standard weighted code, yet in a 4‑bit sequence, it corresponds to the decimal value 13. Its natural binary equivalent, 1101, also evaluates to 13, as shown below:
(1×23) + (1×22) + (0x21) + (1×20) = 8 + 4 + 0 + 1 = 13
Since both representations yield the same decimal result, the conversion is verified as correct.

Figure 2 Table demonstrates step‑by‑step conversion of Gray code 1011 into its natural binary equivalent 1101. Source: Author
Gray code output in rotary encoders
The real difference in a Gray code encoder lies in its output. Instead of returning a binary number that directly reflects the rotor’s position, the encoder produces a Gray code value.
As discussed earlier, Gray code differs fundamentally from binary: it’s not a weighted number system, where each digit consistently contributes to the overall value. Rather, it is an unweighted code designed so that only one bit changes between successive states.
In contrast, natural binary often requires multiple bits to flip simultaneously when incrementing or decrementing, which can introduce ambiguity during transitions. In extreme cases, this ambiguity may cause a controller to misinterpret the encoder’s position, leading to errors in system response. By limiting changes to a single bit, Gray code minimizes these risks and ensures more reliable state detection.
Shown below is a 4-bit Gray code output rotary encoder. As can be seen, it has four output terminals labeled 1, 2, 4, and 8, along with a Common terminal. The numbers 1-2-4-8 represent the bit positions in a 4-bit code, with each terminal corresponding to one output line of the encoder. As the rotor turns, each line switches between high and low according to the Gray code sequence, producing a unique 4-bit pattern for every position.

Figure 3 Datasnip shows terminal ID of the 25LB22-G-Z 4-bit Gray code encoder. Source: Grayhill
The Common terminal serves as the reference connection—typically ground or supply return—against which the four output signals are measured. Together, these terminals provide the complete Gray code output that can be read by a controller or logic circuit to determine the encoder’s angular position.
Sidenote: Hexadecimal output encoders
While many rotary encoders provide Gray code or binary outputs, some devices are designed to deliver signals in hexadecimal format. In these encoders, the rotor position is represented by a 4-bit binary word that maps directly to hexadecimal digits (0–F). This approach simplifies integration with digital systems that already process data in hex, such as microcontrollers or diagnostic interfaces.
Unlike Gray code, hexadecimal outputs do not guarantee single-bit transitions between states, so they are more prone to ambiguity during mechanical switching. However, they remain useful in applications where compact representation and straightforward decoding outweigh the need for transition reliability.
Microcontroller decoding of Gray code
There are several ways to decode Gray codes, but the most common way today is to feed the output bits into a microcontroller and let software handle the counting. In practice, the program reads the signals from the rotary encoder through I/O ports and first converts the Gray code into a binary value. That binary value is then translated into binary-coded decimal (BCD), which provides a convenient format for driving digital displays.
From there, the microcontroller can update a seven-segment display, LCD, or other interface to present the rotor’s position in a clear decimal form. This software-based approach not only simplifies hardware design but also offers flexibility to scale with higher-resolution encoders or integrate additional processing features.
On a personal note, my first experiment with a Gray code encoder involved decoding the outputs using hardware logic circuits rather than software.
This post has aimed to peel back a few layers of Gray codes, offering both context and clarity. Of course, there is much more to explore—and those deeper dives will come in time. For now, I invite you to share your own experiences, insights, or questions about Gray codes in the comments. Your perspectives can spark the next layer of discussion and help shape future explorations.
T. K. Hareendran is a self-taught electronics enthusiast with a strong passion for innovative circuit design and hands-on technology. He develops both experimental and practical electronic projects, documenting and sharing his work to support fellow tinkerers and learners. Beyond the workbench, he dedicates time to technical writing and hardware evaluations to contribute meaningfully to the maker community.
Related Content
- Gray Code Fundamentals – Part 1
- Gray Code Fundamentals – Part 2
- Gray Code Fundamentals – Part 3
- Logic 101 – Part 4 – Gray Codes
- Gray Code Fundamentals – Part 5
The post Gray codes: Fundamentals and practical insights appeared first on EDN.