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.

Field: Logic circuits

Japanese name: グレイコード

Asynchronous FIFO structure Write side (wclk) Read side (rclk) Dual-port RAM R/W on two clocks Write data Write pointer To Gray code Read data Read pointer To Gray code 2-FF sync Empty check 2-FF sync Full check Gray-code pointer In Gray code, only 1 bit changes per increment
Asynchronous FIFO structure · View in diagrams

Converting between binary and Gray code

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.

Why one-bit changes matter in encoders

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.

Gray-coded pointers in asynchronous FIFOs

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.

Gray code in flash memory and Karnaugh maps

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.

Try it: questions about Gray code

Which is a characteristic of Gray code?

  1. AEach decimal digit is represented by 4 bits
  2. BIt detects errors with a parity bit
  3. COnly one bit changes between adjacent values
Show answer and explanation

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.

Why are the write and read pointers of an asynchronous FIFO converted to Gray code before being synchronized into the other domain?

  1. ABecause Gray code halves the number of bits, so fewer synchronizers are needed
  2. BBecause only one bit changes per increment, so even if captured mid-change the result is the correct value from before or after the increment
  3. CBecause Gray-coded signals do not go metastable, so synchronization becomes unnecessary
Show answer and explanation

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.

FAQ

How do you convert binary to Gray code?

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.

Why are FIFO pointers Gray-coded for clock domain crossing?

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.

What is the difference between Gray code and BCD?

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.

Questions that mention Gray code (8)

Gray code appears in 10 practice questions across 3 topics, including Timing analysis and CDC, Logic circuits, SRAM and flash memory.

  1. What is the 4-bit binary value 0110 (decimal 6) converted to Gray code?
  2. In a 16-word-deep asynchronous FIFO with 5-bit Gray code pointers, what is the correct condition for detecting full on the write side?
  3. Why can it be a problem to leave the paths that carry an asynchronous FIFO's Gray code pointers to the other domain unconstrained (false path) just because they are asynchronous?
  4. In multi-level (MLC or TLC) NAND flash, why are the bit patterns assigned to adjacent threshold-voltage states chosen to differ by only one bit?
  5. Each bit of 4-bit data is passed to another clock domain through its own two-stage synchronizer. What problem can occur when the data changes from 0111 to 1000?
  6. In a 16-word-deep asynchronous FIFO, why are the write and read pointers made 5 bits, one bit more than the address?
  7. The write side runs at 250 MHz and the read side at 200 MHz, both transferring one word per cycle. What is the minimum calculated depth for a FIFO receiving a 64-word burst (ignore synchronization delay and round up fractions)?
  8. Two control signals from the same sending domain are each received through a separate two-stage synchronizer and ANDed on the receiving side. What is the problem with this design?

Books on Logic circuits Ad

Learn hands-on: parts and kits

Building real circuits is a good way to check what you learned in the questions.

As an Amazon Associate I earn from qualifying purchases.

Comparisons that include this term

Semiconductor, Electronics and Computing Calculators

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.

Back to the glossary