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)
| Decimal | Binary | Gray Code |
|---|---|---|
| 0 | 0000 | 0 |
| 1 | 0001 | 1 |
| 2 | 0010 | 11 |
| 3 | 0011 | 10 |
| 4 | 0100 | 110 |
| 5 | 0101 | 111 |
| 6 | 0110 | 101 |
| 7 | 0111 | 100 |
| 8 | 1000 | 1100 |
| 9 | 1001 | 1101 |
| 10 | 1010 | 1111 |
| 11 | 1011 | 1110 |
| 12 | 1100 | 1010 |
| 13 | 1101 | 1011 |
| 14 | 1110 | 1001 |
| 15 | 1111 | 1000 |
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
JSON to YAML
Convert JSON into clean, readable YAML instantly.
YAML to JSON
Convert YAML configuration into valid JSON.
JSON to XML
Convert JSON structures into nested XML markup.
HWB to HEX Converter
Convert an HWB color to HEX.
JSON to SQL
Turn a JSON array of objects into SQL INSERT statements.
CSV to XML
Convert CSV rows into structured XML records.
XML to CSV
Flatten repeated XML records into CSV rows.
INI to JSON
Parse INI config sections and keys into JSON.