What is Gray code?
A code in which successive values differ in only one bit. It prevents read errors in rotary encoders and when passing counter values between different clock domains.
Japanese name: グレイコード
A code in which successive values differ in only one bit. It prevents read errors in rotary encoders and when passing counter values between different clock domains.
Japanese name: グレイコード
The usual Gray code is the reflected binary code, built by listing the n-bit sequence, appending its mirror image and prefixing 0 to the first half and 1 to the second. Conversion from binary B is a single expression: G = B XOR (B >> 1), so each Gray bit below the most significant one is the XOR of two neighboring binary bits. Converting back takes a running XOR from the most significant bit down: the top bit is copied, and each lower binary bit is the previous binary bit XOR the current Gray bit. For three bits the sequence is 000, 001, 011, 010, 110, 111, 101, 100.
In plain binary, stepping from 0111 to 1000 flips four bits at once. A rotary or linear absolute encoder reads each bit from its own track, and the transitions on those tracks never line up perfectly, so a reading taken at the boundary can mix old and new bits and report a value far from either position. With Gray code only one track changes at each step, so a reading at the boundary can only be the old or the new value, both of which are correct to within one step.
An asynchronous FIFO passes data between two clock domains, and each side must see the other's read or write pointer to compute full and empty flags. Sending a binary pointer through synchronizer flip-flops is unsafe because several bits may change together and be captured at different clock edges, producing a pointer value that never existed. A Gray-coded pointer changes one bit per increment, so even if that bit is caught mid-transition, the receiving side gets either the old or the new pointer. This works only when the pointer advances by one per clock and the counter wraps at a power of two.
Multi-level cell (MLC) and triple-level cell (TLC) NAND flash store two or three bits as one of four or eight threshold-voltage states. Wear and interference broaden the state distributions, and a misread almost always lands in a neighboring state. Assigning bit patterns in Gray order, such as 111, 110, 100, 101, 001, 000, 010, 011 for TLC, turns each such misread into a single-bit error that error-correcting code handles cheaply. Karnaugh maps use the same ordering on their axes so that physically adjacent cells differ by one variable and can be grouped.
Answer: C. Only one bit changes between adjacent values
Gray code changes exactly one bit between adjacent values and can be computed from binary B as B XOR (B >> 1). Because multiple bits never change at once, it avoids reading spurious intermediate values in rotary-encoder position sensing and when passing FIFO pointers between circuits on different clocks. For FIFO pointers, this requires that the pointer advance by one at a time and wrap around with a power-of-two period. Karnaugh map cells are also ordered in Gray code. Encoding each decimal digit in four bits is BCD.
Answer: B. Because only one bit changes per increment, so even if captured mid-change the result is the correct value from before or after the increment
A binary pointer can change multiple bits at once, as in 0111 to 1000, so synchronizing each bit separately can yield a completely unrelated value. With Gray code only one bit changes per increment, so even if that bit goes metastable and resolves to either the old or new value, the result is the value from before or after the increment. The receiver merely sees a slightly stale value, so the FIFO does not break. The number of bits does not change, and metastability itself can still occur, so synchronizers are still required.
XOR the binary number with itself shifted right by one bit: G = B XOR (B >> 1). The most significant bit stays the same, and every other Gray bit is the XOR of two adjacent binary bits.
A Gray-coded pointer changes only one bit per increment, so a synchronizer that samples it during a transition still yields either the old or the new value. A binary pointer can change many bits at once and be captured as a completely wrong value.
Gray code is an ordering of binary patterns in which successive values differ by one bit. BCD (binary-coded decimal) encodes each decimal digit separately in four bits and has no one-bit-change property.
Gray code appears in 10 practice questions across 3 topics, including Timing analysis and CDC, Logic circuits, SRAM and flash memory.
Building real circuits is a good way to check what you learned in the questions.
As an Amazon Associate I earn from qualifying purchases.
This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.