Gray Code Converter
Convert binary to Gray code, Gray code to binary, or decimal to Gray code (up to 64 bits) with the XOR rule, and see the full reflected Gray code sequence for small bit widths.
How it works
Binary → Gray: G = B ⊕ (B >> 1), i.e. the first Gray bit equals the first binary bit and each next Gray bit is the XOR of two neighbouring binary bits · Gray → binary: B₁ = G₁, then each binary bit = previous binary bit ⊕ current Gray bit
The reflected binary (Gray) code orders numbers so that consecutive values differ in exactly one bit. That prevents glitches when several bits change at once — the reason rotary and linear encoders, Karnaugh maps and asynchronous FIFO pointers use it. Converting to Gray is a single XOR of the number with itself shifted right; converting back is a running XOR from the most significant bit down. Widths up to 64 bits are handled exactly.
Worked example
Binary 1011 (decimal 11): the first Gray bit is 1; then 1 ⊕ 0 = 1, 0 ⊕ 1 = 1, 1 ⊕ 1 = 0, so the Gray code is 1110. Check with the formula: 1011 ⊕ 0101 = 1110. In the 4-bit sequence, 10 (1111) → 11 (1110) → 12 (1010) each change one bit.
Sources: Gray code — Wikipedia · F. Gray, Pulse code communication, US Patent 2,632,058 (1953)
Frequently asked questions
Why is Gray code used in encoders?
When a mechanical or optical encoder moves from one position to the next, only one track changes in Gray code. In plain binary, going from 0111 to 1000 flips four bits, and if they are read mid-transition the position can jump wildly; with Gray code the worst error is one step.
How do I convert Gray code back to binary by hand?
Copy the first (leftmost) bit. Then for each next position, XOR the binary bit you just wrote with the Gray bit below: if the Gray bit is 1, flip the previous binary bit; if 0, repeat it. 1110 → 1, 1⊕1 = 0, 0⊕1 = 1, 1⊕0 = 1 → 1011.
Is there only one Gray code?
Many sequences have the single-bit-change property, but “Gray code” normally means the binary-reflected Gray code produced by G = B ⊕ (B >> 1), which this converter uses. Each n-bit sequence is the (n−1)-bit sequence followed by its mirror image with a leading 1.
What is the link with Karnaugh maps?
K-map rows and columns are labelled in Gray-code order (00, 01, 11, 10) so that neighbouring cells differ in one variable, which is what makes grouping adjacent 1s valid.
Related calculators
This calculator is for educational purposes. Double-check important results before acting on them.