Developer Tools

Gray code converter.

Convert binary or decimal to reflected Gray code, or decode Gray code back to binary, up to 64 bits. See the XOR working and a full reflected Gray table for 1 to 8 bits. Runs in your browser.

Advertisement

Input

Spaces and underscores are ignored. Up to 64 bits.

Result

Enter a value, then press Convert.

Advertisement

Reflected Gray table

How it works

Sources: US 2,632,058, Pulse code communication (Google Patents); Wikipedia, Gray code. Related: Two's Complement Calculator.

Worked example

Computed by running this page’s converter code on 2026-10-26.

Binary 1011 (decimal 11) becomes Gray 1110 (hex E); the working shown is binary = 1011, b >> 1 = 0101, XOR = 1110 (gray). Gray 1110 decodes back to 1011.

3-bit reflected Gray sequence
Decimal01234567
3-bit Gray000001011010110111101100

Each step in the sequence changes exactly one bit, and the last value 100 differs from the first 000 by one bit as well.

Sources and as-of date

As of 2026-10-26. The code follows the reflected binary construction in the original patent; the XOR-and-shift form uses the standard JavaScript operators.

Advertisement

Frequently asked questions

How do you convert binary to Gray code?

XOR the number with itself shifted right by one place. For 1011: b >> 1 is 0101, and 1011 XOR 0101 is 1110.

How do you convert Gray code back to binary?

Keep the top bit, then XOR each following Gray bit with the binary bit just produced. Gray 1110 gives 1011.

Why is Gray code used in position encoders?

Consecutive Gray values differ in exactly one bit, so a reading taken mid-transition is off by at most one step. Plain binary can flip many bits at once: 7 to 8 (0111 to 1000) flips 4.

Is this the only Gray code?

No. Other Gray-type codes exist. This page produces the standard reflected binary Gray code described in Frank Gray’s 1953 patent.