Gray Code Generator
An ordinary binary count changes several bits at once — 0111 to 1000 flips all four — and a sensor reading it mid-change can report a value that was never there. Gray code fixes that by changing exactly one bit at every step, including from the last entry back to the first, which is why rotary encoders use it. The whole sequence comes from three operations: i exclusive-or i shifted right one.
What this generator does
Produces the full reflected binary code for the chosen width, along with which bit changes between each pair of consecutive entries and whether the sequence closes into a cycle.
How to use this tool
- Choose how many bits the code should have.
- Read the sequence, and which bit changes at each step.
- Check the wrap from the last entry back to the first.
- Copy the table for an encoder or a lookup.
Understanding the controls
- How many bits
- Between 2 and 6. Six bits is 64 entries, which is as much as reads usefully in one table.
- Show the full list
- Lists every entry with the bit that changes on the way to the next one.
Common use cases
- Encoder and sensor tables where a mid-change reading must stay valid
- Showing why plain binary counting is unsafe to read asynchronously
- Karnaugh map row and column ordering
- Test data for code that consumes Gray-coded input
- Teaching the reflected binary construction
How this generator works
Each entry is i exclusive-or i shifted right by one, which is the reflected binary construction and needs no lookup table. The check compares every neighbouring pair, including the last back to the first, and requires exactly one differing bit — and confirms the sequence covers every value exactly once.
Randomness and fairness
Nothing here is random. A bit width determines the whole sequence, which is why there is no seed.
For how randomness is produced across the whole site, see how Generate Random works.
Limitations and good to know
- Only the standard reflected binary code; balanced and other Gray codes have different properties.
- Six bits is the ceiling for readability rather than for computation.
- The code is shown as bits rather than as decimal equivalents.
- No decoding tool is provided.
- The table is regenerated each time rather than stored, which costs nothing since it is deterministic.
Privacy and your data
The sequence is computed in your browser from a single number. Nothing is transmitted or kept.
Related generators
- Karnaugh Map Challenge GeneratorGrey-coded Karnaugh maps filled from a real expression, with the largest valid groups found and each one proved to cover only true cells.
- Random Binary Number GeneratorA number shown in binary, alongside its decimal value for reference.
- Base Conversion Question GeneratorConversions between binary, octal, decimal and hexadecimal, with the answer parsed back to prove it is the same number.
- Logic Gate Circuit GeneratorRandom acyclic gate circuits with their equivalent expression and full input-output table, the two required to agree everywhere.