Introduction
Gray code, also called the reflected binary code (RBC), is a binary numeral system where two successive values differ in exactly one bit. Frank Gray patented it in 1947 at Bell Labs for use in pulse-code modulation encoders, where mechanical brush contacts on a rotating shaft would otherwise produce spurious intermediate codes. Today Gray code appears in digital rotary encoders, Karnaugh maps, genetic algorithms, and the tower of Hanoi puzzle. This tool converts between binary and Gray code for any non-negative integer, entirely in your browser.
What this tool does
- Converts a decimal or binary integer to its Gray code representation.
- Converts a Gray code value back to the original binary integer.
- Displays the result in binary, decimal, and hexadecimal forms.
- Handles arbitrarily large inputs using BigInt arithmetic.
- Updates the result in real time as you type, with a copy-to-clipboard button.
How this tool works
Enter a non-negative integer in decimal, binary (prefixed with `0b`), or hex (prefixed with `0x`) form. The tool computes the Gray code by XORing the input with itself right-shifted by one bit (`g = n ^ (n >> 1)`). The inverse conversion reverses this by repeatedly XORing the Gray code with its own right-shifted value until it stabilizes. All arithmetic uses BigInt so values of any width are supported. The result is shown in three bases simultaneously.
How Gray code works
Frank Gray introduced the reflected binary code in a 1947 Bell Labs patent (US 2,632,058) to solve a problem with shaft-angle encoders: a standard binary encoder transitioning from 0111 (7) to 1000 (8) would briefly read 1111 or 0000 if the brushes were misaligned, producing wild errors. Gray code guarantees that only one bit changes per step, so any intermediate reading is either the old or the new value. The construction is recursive: the n-bit Gray code is the (n-1)-bit code followed by its reverse with the top bit set. The same property makes Gray code useful for Karnaugh maps (where adjacent cells differ by one variable) and for the tower of Hanoi (where the disk to move at step k is the position of the changing bit in Gray code of k).
How to use this tool
- Choose Convert to Gray or Convert from Gray using the mode toggle.
- Enter an integer in decimal, binary (`0b1010`), or hex (`0xff`) form.
- The result appears instantly in binary, decimal, and hex.
- Use Copy to copy any of the three representations to your clipboard.
- Switch modes or edit the input at any time; the output updates automatically.
Real-world examples
Converting 7 to Gray code
Input: `7` (binary `0111`). Output: `0100` (decimal 4). Note that only one bit differs between consecutive Gray codes: 6 → 0101, 7 → 0100.
Converting Gray code 0100 back to binary
Input: `0100` (Gray). Output: `0111` (decimal 7). The inverse conversion repeatedly XORs the Gray code with its own right-shifted value.
Large values with BigInt
Input: `0xdeadbeef`. The tool computes the 32-bit Gray code instantly using BigInt, demonstrating support for arbitrarily wide integers.
Comparison with similar methods
| Method | Complexity | Typical use |
|---|---|---|
| Gray code | O(log n) — one shift and one XOR | Rotary encoders, Karnaugh maps, genetic algorithms |
| Binary | O(log n) — direct representation | General-purpose arithmetic and storage |
| One-hot | O(n) — one bit set per state | Finite state machines, fast equality checks |
| Johnson counter | O(n) — circular shift | Clock dividers, state machines with simple decoders |
Limitations or considerations
Gray code is not a cipher and provides no confidentiality. The conversion is a bijection on non-negative integers, so it is lossless and reversible. Negative integers are not supported because Gray code is defined on the natural numbers. The tool does not generate full Gray code sequences for a given bit width; for that, use a dedicated sequence generator or write a loop calling the converter for each index.
Frequently asked questions
Why is it called the reflected binary code?
Because the n-bit Gray code is built by writing the (n-1)-bit code, then writing it again in reverse order with the top bit set. Each new bit width 'reflects' the previous one.
Is Gray code used in modern hardware?
Yes. Rotary and magnetic shaft encoders, Karnaugh-map minimization in logic synthesis, and some low-power bus encodings use Gray code to minimize switching activity.
Can Gray code be used for error detection?
Only for single-bit transitions. It guarantees that adjacent states differ by one bit, which prevents spurious intermediate readings, but it is not a general error-correcting code.
How is Gray code related to the tower of Hanoi?
The disk to move at step k is the index of the bit that changes in the Gray code of k. This gives an optimal solution to the puzzle in 2^n - 1 moves.
Conclusion
The Gray code converter is a fast, BigInt-backed tool for translating between binary and the reflected binary code used in rotary encoders, Karnaugh maps, and the tower of Hanoi. With real-time conversion, multi-base output, and a fully client-side implementation, it is a handy reference for anyone working with single-bit-transition encodings.