Convert Gray Code to Binary
Decode Gray code (reflected binary) to standard binary, decimal, or hex. BigInt-safe, per-line errors, step-by-step XOR breakdown. Free, offline, 100% client-side.
Decode reflected binary (Gray code) back to standard binary, decimal, or hex using the MSB-first XOR chain. Accepts 0b prefixes, spaces, and underscore separators. BigInt-safe for any bit width.
How to Use Convert Gray Code to Binary
- Paste your Gray code values - one per line. You can include a
0bprefix, spaces, or underscores (1011_0110); the tool strips them before decoding. - Pick an output format: standard binary (default), decimal value, hexadecimal, or all three side by side for spreadsheet paste.
- Toggle 4-bit grouping if you want the binary output split into nibbles like
1101 1011for easier reading of long sequences. - Press Convert (or Ctrl+Enter / Cmd+Enter). The tool auto-converts 200 ms after you stop typing too.
- Check the breakdown panel: each decoded value shows its decimal and hex counterparts, and - if you tick "Show XOR step trace" - the bit-by-bit XOR chain used to derive it.
- Handle errors per line: if one input has an invalid character, only that line shows a red error row - the rest still decode successfully.
- Copy or download your results. Copy uses the modern Clipboard API with an
execCommandfallback for older browsers; Download saves a plain.txtfile.
Frequently Asked Questions
How does Gray-to-binary conversion work, exactly?
The algorithm walks left-to-right. The most significant bit of the binary result equals the most significant bit of the Gray code (binary[0] = gray[0]). Every subsequent bit is the XOR of the previous binary bit with the current Gray bit: binary[i] = binary[i-1] ⊕ gray[i]. Example: Gray 111 → bit 1 keep 1, bit 2 1⊕1 = 0, bit 3 0⊕1 = 1, so binary is 101 (decimal 5).
Can I paste Gray codes with prefixes or separators?
Yes. The tool accepts 0b1011 (C/Python-style binary prefix), 1011 0110 (spaces), and 1011_0110 (underscore separators, as used in modern languages like Rust and JavaScript). All three get normalized to 10110110 before decoding.
Is BigInt used for decimal and hex output?
Yes. The decoder itself is pure string/XOR math (no integer overflow possible), and the decimal/hex conversions use JavaScript’s native BigInt. That means a 256-bit Gray code converts to its exact decimal value without losing precision, which ordinary Number arithmetic would at 253.
What happens if one line in my batch is invalid?
Only that line fails. The tool processes each line independently, so a stray character like 1021 becomes a single red row in the breakdown (“Line 3: invalid character ‘2’ at position 2”) while every other valid line still decodes. The stats row tells you how many succeeded versus failed.
Why would I want the XOR step trace?
Because understanding Gray code is mostly about understanding that single XOR chain. Toggle “Show XOR step trace” and every decoded line prints its steps like bit 1: keep 1 → 1 · bit 2: 1⊕1 → 0 · bit 3: 0⊕1 → 1. It’s the fastest way to verify homework answers or teach students why reflected binary works.
Does this work with rotary encoder output?
Absolutely – that’s one of the primary use cases. Absolute rotary encoders and shaft position sensors typically emit Gray code specifically so that only one bit flips between adjacent positions, eliminating transient errors during the transition. Paste the raw encoder reading here to recover the integer position.
Is my data private when I use this tool?
Yes. Every byte of processing happens in your browser’s JavaScript runtime – nothing is uploaded, logged, or stored on any server. The conversion still works even if you disconnect from the internet after the page loads. Verify it yourself by opening the browser’s Network tab while decoding.
What’s the difference between this tool and a binary-to-Gray-code converter?
This one is the decoder – it assumes the input is already Gray code and gives you back the binary it represents. A binary-to-Gray-code converter (the encoder) does the opposite: gray[i] = bin[i] ⊕ bin[i-1]. The two are perfect inverses, so round-tripping any value through both produces the original.
What does the “4-bit grouping” option do?
It inserts a space every 4 bits in the binary output, counting from the right, so 11011011 becomes 1101 1011. It also left-pads the value to the next multiple of 4 so the groups line up neatly. This is visual-only – the underlying value and the decimal/hex outputs are unaffected.
Is there a limit on the bit width I can decode?
Practically no. The XOR decoder is O(n) in bit length and uses plain strings, so a 1,024-bit Gray code decodes in microseconds. Decimal and hex output use BigInt, which has no fixed size ceiling. The breakdown panel caps display at 50 rows for performance, but the full output textarea always contains every converted line.