Gray Code Converter
Convert between binary and reflected Gray code, with the per-bit XOR working shown and a full sequence table.
000010110000111011140xB0xEBit-by-bit derivation
| Bit | Binary | Gray | Rule |
|---|---|---|---|
| 7 | 0 | 0 | MSB copies straight across: g7 = b7 = 0 |
| 6 | 0 | 0 | g6 = b6 XOR b7 = 0 XOR 0 = 0 |
| 5 | 0 | 0 | g5 = b5 XOR b6 = 0 XOR 0 = 0 |
| 4 | 0 | 0 | g4 = b4 XOR b5 = 0 XOR 0 = 0 |
| 3 | 1 | 1 | g3 = b3 XOR b4 = 1 XOR 0 = 1 |
| 2 | 0 | 1 | g2 = b2 XOR b3 = 0 XOR 1 = 1 |
| 1 | 1 | 1 | g1 = b1 XOR b2 = 1 XOR 0 = 1 |
| 0 | 1 | 0 | g0 = b0 XOR b1 = 1 XOR 1 = 0 |
Single-bit-change property
Exactly one bit differs from each neighbour - the property that makes Gray code safe to sample mid-transition.
5-bit Gray code sequence
Read down the Gray column and watch which bit changes on each row - it is never more than one, including on the wrap from the last row back to the first.
| Decimal | Binary | Gray | Bit that changed |
|---|---|---|---|
| 0 | 00000 | 00000 | bit 4 (wrap) |
| 1 | 00001 | 00001 | bit 0 |
| 2 | 00010 | 00011 | bit 1 |
| 3 | 00011 | 00010 | bit 0 |
| 4 | 00100 | 00110 | bit 2 |
| 5 | 00101 | 00111 | bit 0 |
| 6 | 00110 | 00101 | bit 1 |
| 7 | 00111 | 00100 | bit 0 |
| 8 | 01000 | 01100 | bit 3 |
| 9 | 01001 | 01101 | bit 0 |
| 10 | 01010 | 01111 | bit 1 |
| 11 | 01011 | 01110 | bit 0 |
| 12 | 01100 | 01010 | bit 2 |
| 13 | 01101 | 01011 | bit 0 |
| 14 | 01110 | 01001 | bit 1 |
| 15 | 01111 | 01000 | bit 0 |
| 16 | 10000 | 11000 | bit 4 |
| 17 | 10001 | 11001 | bit 0 |
| 18 | 10010 | 11011 | bit 1 |
| 19 | 10011 | 11010 | bit 0 |
| 20 | 10100 | 11110 | bit 2 |
| 21 | 10101 | 11111 | bit 0 |
| 22 | 10110 | 11101 | bit 1 |
| 23 | 10111 | 11100 | bit 0 |
| 24 | 11000 | 10100 | bit 3 |
| 25 | 11001 | 10101 | bit 0 |
| 26 | 11010 | 10111 | bit 1 |
| 27 | 11011 | 10110 | bit 0 |
| 28 | 11100 | 10010 | bit 2 |
| 29 | 11101 | 10011 | bit 0 |
| 30 | 11110 | 10001 | bit 1 |
| 31 | 11111 | 10000 | bit 0 |
Why Gray code exists
Ordinary binary counting flips several bits at once. Going from 7 to 8 in four bits takes 0111 to 1000 - every bit moves. If something reads that value while the bits are still settling, it can see any of the sixteen possible patterns, including values nowhere near either 7 or 8. On a rotary encoder that means a shaft sitting between two positions can report a wild third one.
Gray code makes that impossible by construction: consecutive values differ in exactly one bit, so a mid-transition read can only ever return the old value or the new one. The encoding costs nothing - it is one XOR each way - and it removes an entire class of glitch.
The same argument applies wherever a multi-bit value crosses a boundary that is not synchronous with it. Asynchronous FIFO read and write pointers are Gray-coded before crossing into the other clock domain for exactly this reason: the receiving domain samples them at an arbitrary moment, and only a single-bit-change encoding guarantees the sample is one of the two legitimate values.
Where you will meet it
Absolute rotary encoders
The concentric tracks on an absolute encoder disc are a Gray code, so only one track changes at each boundary and no mechanical tolerance can produce a bogus position.
Clock-domain crossing
Async FIFO pointers are Gray-coded before being synchronised into the far clock domain, so a metastable sample resolves to either the old or the new pointer, never a third value.
Karnaugh maps
K-map rows and columns are labelled in Gray order so adjacent cells differ in one variable, which is what makes the visual grouping of terms valid.
Flash ADCs and DACs
Thermometer-to-binary decoders often route through Gray code so a comparator sitting right on a threshold cannot produce a large output error.
Code Examples
#include <stdint.h>
uint32_t binary_to_gray(uint32_t n) {
return n ^ (n >> 1);
}
uint32_t gray_to_binary(uint32_t g) {
g ^= g >> 16;
g ^= g >> 8;
g ^= g >> 4;
g ^= g >> 2;
g ^= g >> 1;
return g;
}
/* Each fold XORs the accumulator, not the original value. */Frequently Asked Questions
What is Gray code?
A binary numbering where consecutive values differ in exactly one bit. Counting 0,1,2,3 in ordinary binary goes 00, 01, 10, 11 - the step from 1 to 2 flips both bits. In Gray code it goes 00, 01, 11, 10, and only ever one bit moves at a time.
How do I convert binary to Gray code?
XOR the value with itself shifted right one place: g = b ^ (b >> 1). The most significant bit passes through unchanged and every lower Gray bit is the XOR of the binary bit at that position with the one above it.
How do I convert Gray code back to binary?
Each binary bit is the XOR of every Gray bit at or above it. In code that is the doubling fold b ^= b >> 1; b ^= b >> 2; b ^= b >> 4; and so on - and it has to fold the accumulator each round, not shifts of the original Gray value.
Why does Gray code matter for rotary encoders?
Because the bits of a physical encoder never change at exactly the same instant. In ordinary binary, the transition from 0111 to 1000 can be read mid-flight as anything from 0000 to 1111. With Gray code only one track changes per step, so the worst case is reading the old value or the new one - never a spurious third position.
Where else is Gray code used?
Asynchronous FIFO pointers crossing clock domains, flash ADC thermometer decoding, Karnaugh map row and column ordering, some genetic algorithm encodings, and error-tolerant position sensing generally. Anywhere sampling a multi-bit value mid-transition would otherwise produce garbage.
Is Gray code the same as binary-reflected Gray code?
The standard one is. 'Reflected' describes how the sequence is built: take the n-bit list, mirror it, prefix 0 to the first half and 1 to the second. Other single-bit-change codes exist, but 'Gray code' unqualified means the reflected binary one.
Does Gray code preserve arithmetic?
No. You cannot add two Gray-coded values directly - convert to binary, do the arithmetic, convert back. Gray code is a transmission and sensing encoding, not an arithmetic one.
Is the Gray code sequence cyclic?
Yes. The last value in an n-bit Gray sequence differs from the first by exactly one bit too, which is what makes it correct for a rotary encoder that wraps around past zero.
Related Tools
BCD Converter
Convert decimal to packed and unpacked binary-coded decimal and back, with invalid-nibble detection.
Binary to Decimal Converter
Convert binary numbers to decimal (base 10) instantly.
Bitmask Generator & Register Workbench
Build, paste and visualize bitmasks at 8, 16, 32 or 64 bits - then generate the set, clear, toggle and field-extract code for them.
This tool runs entirely in your browser - nothing you enter is uploaded or stored.