What Is Gray Code?
Gray code, also known as reflected binary code, is a way of ordering binary numbers so that two successive values differ in exactly one bit. Ordinary binary does not have this property: counting from 7 (0111) to 8 (1000) flips all four bits at once. If those bits do not switch at precisely the same instant, a circuit can momentarily read a completely wrong value. Gray code, named after Bell Labs researcher Frank Gray, eliminates that hazard by guaranteeing a single-bit transition every step. This converter goes both ways — binary to Gray and Gray to binary — and shows the XOR working bit by bit.
How to Use This Converter
1. Pick a direction — Binary → Gray Code, or Gray Code → Binary. 2. Type your bits (0s and 1s only; anything else is filtered out). The converted value, the decimal equivalent and the hexadecimal appear instantly. 3. Follow the XOR diagram to see the first bit copied down in green and each later bit produced by an XOR. 4. Scroll the Gray code sequence table to see the full count, with your current value highlighted and every changed bit marked in orange.
How to Convert Binary to Gray Code
The rule has two parts: copy the most significant bit unchanged, then XOR each pair of adjacent binary bits. As a formula, G = B ⊕ (B >> 1) — XOR the number with itself shifted right by one.
Example — binary 1011 to Gray code
G₃ = B₃ = 1
G₂ = B₃ ⊕ B₂ = 1 ⊕ 0 = 1
G₁ = B₂ ⊕ B₁ = 0 ⊕ 1 = 1
G₀ = B₁ ⊕ B₀ = 1 ⊕ 1 = 0
So binary 1011 (decimal 11) becomes Gray code 1110.
How to Convert Gray Code to Binary
Going back is subtly different — it uses a running XOR rather than adjacent input bits. Copy the most significant Gray bit to become the first binary bit, then each following binary bit is the XOR of the binary bit you just produced and the current Gray bit: B[i] = B[i−1] ⊕ G[i].
Example — Gray code 1110 back to binary
B₃ = G₃ = 1
B₂ = B₃ ⊕ G₂ = 1 ⊕ 1 = 0
B₁ = B₂ ⊕ G₁ = 0 ⊕ 1 = 1
B₀ = B₁ ⊕ G₀ = 1 ⊕ 0 = 1
Gray code 1110 is binary 1011 — the original value, decimal 11.
Gray Code Table (3-Bit and 4-Bit)
| Decimal | Binary (3-bit) | Gray (3-bit) | Binary (4-bit) | Gray (4-bit) |
|---|---|---|---|---|
| 0 | 000 | 000 | 0000 | 0000 |
| 1 | 001 | 001 | 0001 | 0001 |
| 2 | 010 | 011 | 0010 | 0011 |
| 3 | 011 | 010 | 0011 | 0010 |
| 4 | 100 | 110 | 0100 | 0110 |
| 5 | 101 | 111 | 0101 | 0111 |
| 6 | 110 | 101 | 0110 | 0101 |
| 7 | 111 | 100 | 0111 | 0100 |
| 8 | — | — | 1000 | 1100 |
Read down either Gray column and you will see only one bit flip per step — including the jump from 3 to 4 and from 7 to 8, where plain binary changes several bits at once.
Why Is Gray Code Used?
The single-bit-change property prevents transient misreads. Real switches and sensors never change at exactly the same nanosecond, so a binary encoder passing from 0111 to 1000 might briefly output 1111 or 0000 — wildly wrong values. With Gray code no such glitch is possible. Common uses include:
- Rotary and linear position encoders — the classic application, where a misread angle could be dangerous.
- Karnaugh maps — the rows and columns are labelled in Gray code order (00, 01, 11, 10) so that adjacent cells differ by one variable. See it in the K-Map Solver.
- Analog-to-digital converters and digital communications, where it limits error magnitude.
- Clock-domain crossing in FPGA and ASIC design, where multi-bit counters must be sampled safely.
- Error correction and mathematical puzzles such as the Towers of Hanoi.
Gray Code vs Binary
Gray code and binary represent the same underlying values, but with different bit patterns — so the pattern 1110 means 14 as plain binary but 11 as Gray code. Binary is the right choice for arithmetic, because adders and ALUs expect it. Gray code is the right choice wherever a value is physically sensed or transmitted and a momentary multi-bit glitch would be harmful. Note that XOR is the engine of the whole conversion — explore the gate in the Logic Gate Calculator, switch bases in the Number System Converter, or handle signed values in the Two’s Complement Calculator.
Frequently Asked Questions
What is Gray code?
A binary numbering system (also called reflected binary code) in which two successive values differ in exactly one bit, avoiding the multi-bit transitions that can cause misreads in hardware.
How do you convert binary to Gray code?
Copy the most significant bit unchanged, then XOR each pair of adjacent binary bits. As a formula: G = B ⊕ (B >> 1). Binary 1011 becomes Gray 1110.
How do you convert Gray code to binary?
Copy the most significant Gray bit, then each following binary bit is the XOR of the previous binary bit and the current Gray bit — a running XOR. Gray 1110 becomes binary 1011.
Why is Gray code used?
Because only one bit changes between consecutive values, it prevents transient errors when several bits would otherwise switch at once. It is used in position encoders, ADCs, Karnaugh maps and clock-domain crossing.
What is the Gray code for a decimal number?
Convert the decimal to binary first, then apply the binary-to-Gray rule. Decimal 11 is binary 1011, which becomes Gray code 1110.
Is Gray code the same as binary?
No. They encode the same values with different bit patterns. The pattern 1110 read as binary is 14, but read as Gray code it represents 11.