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.
Input
Result
Enter a value, then press Convert.
Reflected Gray table
How it works
- Binary to Gray: gray = b XOR (b >> 1). Shift the binary number right one place and XOR it with the original. The top bit is unchanged.
- Gray to binary: the binary bits are the running (prefix) XOR of the Gray bits from the most significant bit down. Example: Gray 1110 gives 1, 1^1=0, 0^1=1, 1^0=1, so binary 1011.
- Hamming-distance-1 property: in the reflected code, consecutive values differ in exactly one bit, and the last value differs from the first by one bit too. Ordinary binary can flip many bits at once (0111 to 1000 flips four), which is why Gray code is used for position encoders where a misread mid-transition would give a large error.
- Origin: Frank Gray of Bell Labs described the reflected binary code in US patent 2,632,058, "Pulse code communication", filed 13 November 1947 and granted 17 March 1953.
- Scope: this is the standard reflected binary Gray code. Other Gray-type codes exist and are not produced here. Uses BigInt, so 64-bit values are exact. Nothing is sent anywhere.
Sources: US 2,632,058, Pulse code communication (Google Patents); Wikipedia, Gray code. Related: Two's Complement Calculator.