Skip to content
ZeroServer.tools

Gray Code Converter

Convert between binary, Gray code, and decimal. Gray code changes only one bit between consecutive values.

Output
1111
Reference table (0–15)
DecimalBinaryGray Code
000000
100011
2001011
3001110
40100110
50101111
60110101
70111100
810001100
910011101
1010101111
1110111110
1211001010
1311011011
1411101001
1511111000

What is Gray code?

Gray code (reflected binary code) is a binary numeral system where consecutive values differ by exactly one bit. This property makes it valuable in digital circuits, rotary encoders, and error correction, because single-bit transitions prevent transient errors during state changes. The conversion from binary to Gray code is: G[0] = B[0] and G[i] = B[i−1] XOR B[i].

Built and maintained by Meet Shah · Last updated

What this tool is used for

  • Working with a rotary encoder whose output is Gray-coded so only one bit changes per step.
  • Checking why a position sensor reading is stable across a transition.
  • Converting a Gray code value from a datasheet into ordinary binary.
  • Producing a Gray code sequence for a teaching example about glitch-free transitions.
  • Debugging a state machine that uses Gray coding to avoid intermediate states.

Frequently Asked Questions

What problem does Gray code solve?
Consecutive values differing in exactly one bit. In plain binary 3 → 4 flips all three bits (011 → 100), and no physical device flips bits simultaneously — so a sensor read mid-transition can return 111 or 000, values wildly wrong. In Gray code one bit moves, so a mistimed read is off by one.
What are the conversion formulas?
Binary to Gray is G[0] = B[0] and G[i] = B[i−1] XOR B[i] — each bit XORed with the one above it. Coming back is cumulative: B[0] = G[0] and B[i] = B[i−1] XOR G[i], so decoding must run left to right and cannot be done bit-independently.
Where is it actually used?
Rotary and linear position encoders, where a misread angle reads as a mechanical fault; Karnaugh maps, whose adjacency depends on the single-bit property; and clock-domain crossing in digital design, where a multi-bit counter sampled by another clock could be caught mid-change.
How large a number can it handle?
Any size. The decimal path goes through BigInt — an earlier version used `>>> 0`, which truncates to 32 bits, so anything above 4,294,967,295 was silently wrong. The bit walks operate on the binary string directly and were always exact at any width.
Why is it called reflected binary code?
Because of how the sequence is built: take the n-bit list, mirror it, prefix 0 to the original half and 1 to the reflected half. That reflection is what guarantees the single-bit property across the join, and it is why the code wraps — the last value differs from the first by one bit too.

Common errors and gotchas

  • Treating Gray code as a numeric base, when it is a re-ordering of binary values rather than a different weighting.
  • Comparing Gray code values with ordinary arithmetic, which does not respect their ordering.
  • Converting bit by bit, when the transformation depends on the bits above each position.
  • Losing the width, which changes the code for the same value.
  • Assuming the reflected form is the only Gray code, when other single-change orderings exist.

Related Converters tools

Private & free — this tool runs entirely in your browser.

IndieKitShip your Next.js startup in days.affiliate