1001Ferramentas
📋 Dev

Gray Code Table Generator

List all 2^N Gray codes for N bits (1 to 8). Shows binary side-by-side and highlights the 1-bit transition.

iBinaryGray

Gray code: counting one bit at a time

In ordinary binary counting, going from 7 to 8 changes four bits at once: 0111 becomes 1000. In a real circuit those bits do not switch at the same instant, and during the transition the reading can be anything — including a number matching neither value. Gray code removes the problem at the root: between two consecutive values, exactly one bit changes.

Choose the number of bits, from 1 to 8, and the page builds the full table with the decimal, ordinary binary and matching Gray value. The conversion is surprisingly simple: take the exclusive-or of the number with itself shifted one bit right. The way back is less direct, because each bit of the result depends on all the more significant bits already decoded.

Where this genuinely matters is in a rotary encoder, the disc reporting a shaft's position. With ordinary binary, reading at the wrong instant returns an absurd position; with Gray, the worst case is reading a neighbour. The same idea appears in Karnaugh maps, where Gray ordering makes adjacent cells differ in a single variable, and in analogue-to-digital converters.

Frequently asked questions

Is there only one Gray code?
No. What the table shows is the reflected binary Gray code, the most widely used and the one the exclusive-or formula produces. Other sequences also change one bit per step — the condition defines a family, not a unique answer.
Does the sequence close the loop?
It does: the last value in the table also differs from the first by a single bit. That cyclic property is exactly what makes the code suitable for angular position, where passing 360 degrees returns to the start with no discontinuity.
How do I convert from Gray back to binary?
Bit by bit, from most significant to least: the first bit is copied, and each following bit of the result is the exclusive-or of the matching Gray bit with the previously computed binary bit. That is why decoding is sequential while encoding comes out of a single operation.

Related Tools