Generate a Gray Code Sequence
Select the number of bits to generate all \(2^n\) codes in the standard reflected Gray code order. Larger bit widths produce exponentially more rows.
Generated sequence
Transition checks compare each displayed code with the previous code in the full sequence. The first displayed row has no previous row within this view when the selected range begins after index zero.
What Is a Gray Code Sequence?
A Gray code is an ordering of binary values in which consecutive code words differ in exactly one bit. The standard binary reflected Gray code is useful when reducing errors caused by multiple bits changing at once. It appears in digital electronics, rotary encoders, communication systems, and algorithms that enumerate combinations.
Unlike ordinary binary counting, where several bits can change during a transition such as 0111 to 1000, Gray code changes just one bit at each step. For an n-bit reflected Gray code, the sequence contains exactly \(2^n\) distinct values.
Example: 3-bit Gray code
In this sequence, every adjacent pair differs in one position. The last value, 100, also differs from the first value, 000, by one bit, making the standard reflected sequence cyclic.
Gray Code Formula
The Gray code value at zero-based index \(i\) can be computed directly from the index using an exclusive OR (XOR) operation:
The right-shift operation moves the binary digits of the index one place to the right. XOR combines the original value with that shifted value. When represented with a fixed bit width, the resulting value is the corresponding Gray code word.
Worked example for index 6
Shift right: 011
XOR result: 101
Therefore, index 6 corresponds to Gray code 101 in the 3-bit sequence. The generator uses the same direct formula for each requested row.
How the reflected sequence is built
The sequence can also be built recursively. Start with the zero-bit sequence containing an empty word. For each additional bit, prefix 0 to the existing sequence, then prefix 1 to the existing sequence in reverse order. The number of entries doubles each time a bit is added.
Common Uses of Gray Code
- Rotary encoders: A one-bit change between adjacent positions can reduce ambiguity while reading a moving encoder.
- Digital logic: Gray code is useful in selected counter and state-transition designs.
- Karnaugh maps: Gray ordering helps place adjacent Boolean input combinations next to one another.
- Combinatorial algorithms: Gray sequences can enumerate subsets so that only one membership changes between successive states.
- Analog-to-digital and position systems: Gray-coded representations can help reduce errors caused by simultaneous bit transitions.
Gray code is not a replacement for ordinary binary in every application. Its main advantage is the controlled change between adjacent code words.
Frequently Asked Questions
1. How many values are in an n-bit Gray code sequence?
There are \(2^n\) values. For example, a 3-bit sequence contains 8 code words, while a 4-bit sequence contains 16.
2. Why is Gray code called a single-bit-change code?
Each consecutive code word differs from the previous word in exactly one bit. This property can reduce ambiguity when values change between adjacent states.
3. What formula generates a Gray code?
The standard binary reflected Gray code at index \(i\) is \(G(i)=i \oplus (i \gg 1)\), where XOR is bitwise exclusive OR and the right shift moves bits one position to the right.
4. Does the sequence include leading zeros?
Yes. The generator formats every code to the selected bit width. For example, the 4-bit sequence begins with 0000, 0001, 0011, and 0010.
5. Can I generate a partial Gray code sequence?
Yes. Set the starting index and maximum row count to display a selected section. The starting index is zero-based, so the first code has index 0.
6. Can I export the generated sequence?
Yes. Use Copy displayed sequence to copy the visible table as tab-separated text, or Download CSV to save the displayed rows in a spreadsheet-compatible file.
7. Is Gray code the same as binary code?
No. Both can represent the same number of states when they use the same number of bits, but their code words appear in different orders. Gray code arranges them so adjacent entries change one bit.
Related Binary and Gray Code Tools
Use these related Binarios.org tools to convert values between binary representations and other number systems.