Binary to Gray Code Converter
Free binary to Gray code converter. Convert binary ↔ Gray at 3 to 8-bit and 16-bit widths with step-by-step XOR working, bit-by-bit visualization, truth table, and hex/decimal outputs.
Short values are zero-padded to 4 bits. Longer values are rejected.
Gray Code
1110
= 14 (decimal) = 0xE
Bit Layout
4-bit conversion, MSB on the left
Each Gray bit (except MSB) = XOR of the binary bit directly above it and its left neighbour.
All Formats
The result in every representation
Step-by-Step Working
Exact XOR trace this calculator followed, MSB first
- 1
G[3] = B[3] = 1
= 1 - 2
G[2] = B[3] ⊕ B[2] = 1 ⊕ 0
= 1 - 3
G[1] = B[2] ⊕ B[1] = 0 ⊕ 1
= 1 - 4
G[0] = B[1] ⊕ B[0] = 1 ⊕ 1
= 0
4-Bit Truth Table
Every input value and its Gray code equivalent
| Dec | Binary | Gray |
|---|---|---|
| 0 | 0000 | 0000 |
| 1 | 0001 | 0001 |
| 2 | 0010 | 0011 |
| 3 | 0011 | 0010 |
| 4 | 0100 | 0110 |
| 5 | 0101 | 0111 |
| 6 | 0110 | 0101 |
| 7 | 0111 | 0100 |
| 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?
A binary encoding where consecutive numbers differ by exactly one bit
Gray code, also called reflected binary code, was patented by Frank Gray in 1953. It is a non-weighted binary encoding with one defining property: any two consecutive values differ in exactly one bit. Walk 0 → 1 → 2 → 3 in standard binary and you flip one, two, then one bit. Walk it in Gray code and every step flips a single bit, always.
That unit-distance guarantee is why Gray code shows up wherever a reading is captured while a value is still changing. Rotary encoders, Karnaugh maps, asynchronous FIFO pointers, and Gray-coded QAM/PAM constellations all depend on it.
Binary → Gray Code
G[n−1] = B[n−1] • G[i] = B[i+1] ⊕ B[i]
Unit-distance
Every increment flips exactly one bit, so mid-transition reads never glitch.
Reflected
Second half mirrors the first with a leading 1. The name comes from this property.
Cyclic
The last and first values also differ by one bit, wrapping cleanly.
How to Convert Binary to Gray Code
A three-step XOR procedure you can do on paper in seconds
Step 1: Pad to your chosen bit width
Write the binary value with leading zeros so every position is defined. At 4-bit, decimal 5 is 0101 (not 101) because the missing MSB changes every downstream XOR.
Step 2: Copy the MSB unchanged
The leftmost Gray bit is identical to the leftmost binary bit. This bit anchors the entire reflected sequence.
Step 3: XOR every adjacent binary pair
For each remaining position, XOR the bit to its left with the bit at that position. Same-same → 0, different → 1. Read the Gray code left to right when done.
Worked Example: Binary 1011 ↔ Gray 1110
Every XOR shown, both directions verified
Binary → Gray (1011)
- G[3] = B[3] = 1
- G[2] = 1 ⊕ 0 = 1
- G[1] = 0 ⊕ 1 = 1
- G[0] = 1 ⊕ 1 = 0
- Result: 1110
Gray → Binary (1110)
- B[3] = G[3] = 1
- B[2] = 1 ⊕ 1 = 0
- B[1] = 0 ⊕ 1 = 1
- B[0] = 1 ⊕ 0 = 1
- Result: 1011 ✓
One-Liner Shortcut
Gray = Binary ⊕ (Binary >> 1)
4-Bit Binary to Gray Code Truth Table
Every value from 0 to 15 with the bit that changed from the row above
| Decimal | Binary | Gray | Bit changed |
|---|---|---|---|
| 0 | 0000 | 0000 | — |
| 1 | 0001 | 0001 | G₀ |
| 2 | 0010 | 0011 | G₁ |
| 3 | 0011 | 0010 | G₀ |
| 4 | 0100 | 0110 | G₂ |
| 5 | 0101 | 0111 | G₀ |
| 6 | 0110 | 0101 | G₁ |
| 7 | 0111 | 0100 | G₀ |
| 8 | 1000 | 1100 | G₃ |
| 9 | 1001 | 1101 | G₀ |
| 10 | 1010 | 1111 | G₁ |
| 11 | 1011 | 1110 | G₀ |
| 12 | 1100 | 1010 | G₂ |
| 13 | 1101 | 1011 | G₀ |
| 14 | 1110 | 1001 | G₁ |
| 15 | 1111 | 1000 | G₀ |
Notice: every row changes exactly one Gray bit from the row above. This is the defining unit-distance property. Standard binary would flip three bits going 3 → 4, which is exactly why Gray code is used for encoders and counters that might be sampled mid-transition.
Gray Code to Binary: The Reverse Direction
A running XOR from the MSB down the word
Going Gray → Binary is not quite symmetric. Converting into Gray uses adjacent binary pairs; converting out of Gray uses the current Gray bit plus the previously computed binary bit. The MSB still copies across, but each subsequent binary bit is a running XOR down the word.
Gray → Binary
B[n−1] = G[n−1] • B[i] = B[i+1] ⊕ G[i]
Hardware / Software One-Liner
Binary = Gray ⊕ (Gray >> 1) ⊕ (Gray >> 2) ⊕ … ⊕ (Gray >> n−1)
Shift Gray right by every power up to n−1 and XOR them together. This is the running-XOR formula folded into a single expression, and it is how most hardware decoders are actually built.
Where Gray Code Is Used
Real-world domains where unit-distance encoding is not optional
Rotary & Linear Encoders
Absolute optical encoders etch Gray code tracks onto the disc so a sensor read mid-rotation can only disagree with the true position by one bit, never by an arbitrary value.
Karnaugh Maps
K-maps order rows and columns in Gray sequence so adjacent cells differ by one variable. That is the whole reason visual grouping reveals Boolean minimizations.
Clock-Domain Crossing
Asynchronous FIFO pointers are Gray-coded before crossing clock boundaries so a setup/hold violation can only flip a single bit, preventing multi-bit metastability.
Digital Communications
PAM and QAM constellations are Gray-coded so the most likely symbol errors (adjacent points) cause only a single bit flip at the receiver, minimizing bit-error rate.
Low-Power FSMs
Gray-coded state labels minimize switching activity per clock. Fewer flip-flops toggle, which lowers dynamic power in mobile SoCs and battery-sensitive designs.
Puzzles & Combinatorics
The Tower of Hanoi and Chinese Rings move sequences both follow Gray code. It also enumerates subsets of {1..n} with a single element changing at each step.
Common Mistakes
The errors that cost marks on exam day and bugs in production
Forgetting to pad to full bit width. Converting 101 at 4 bits means working with 0101 (not 101), because the missing MSB changes every XOR downstream.
Using AND or OR instead of XOR. Gray conversion uses exclusive-or only: same bits produce 0, different bits produce 1. Nothing else.
Mixing the two formulas. Binary to Gray uses adjacent binary bits, while Gray to Binary uses the previous binary bit with the current Gray bit. They are not interchangeable.
Skipping the MSB copy. Both directions start by copying the most significant bit verbatim. Missing that step shifts every subsequent bit out of place.
Applying Gray arithmetic directly. Gray code is not a weighted number system, so you cannot add or multiply Gray values. Decode to binary first.
Pro Tips
Tricks that save time once you have the fundamentals down
Code in one line: Gray = Binary ^ (Binary >> 1). Works in C, Verilog, Python, and JavaScript, or any language with bitwise XOR and right-shift.
Verify by walking the output values 0..2^n−1 and confirming each consecutive pair differs in exactly one bit. Any step flipping two or more means a bug.
Reflected construction: n-bit Gray is (n−1)-bit Gray followed by (n−1)-bit Gray reversed, with the halves prefixed 0 and 1. Fast for generating tables by hand.
K-map column order 00, 01, 11, 10 is Gray order. That is exactly why adjacent cells differ by one variable and minimization groups pop out visually.
For Gray-to-binary in hardware, the MSB-to-LSB running XOR has the longest dependency path (one XOR per bit of logic depth). Flatten it with Sklansky-style prefix reductions when clock frequency matters.
Frequently Asked Questions
Common questions and detailed answers
Embed Binary to Gray Code Converter
Add this calculator to your website or blog for free.
You Might Also Like
Related calculators from other categories
Last updated Apr 17, 2026