Free Online Engineering Tools

Gray Code Converter — Binary to Gray & Gray to Binary

Convert binary to Gray code and Gray code back to binary, with the XOR working shown bit by bit. Also see the decimal equivalent and the full n-bit Gray code sequence.

1Direction & Input
🔄Conversion Direction
BINBinary Input4 bits
GRAYGray Code Outputresult
DECDecimal Valueof the binary
💡
Pro Tip
The rule is G = B ⊕ (B >> 1) — copy the first bit, then XOR each pair of neighbours. Going back uses a running XOR instead.
2XOR Working — Bit by Bit
3Gray Code Sequence (4-bit)
Notice that only one bit changes between each row — that is the whole point of Gray code. Changed bits are highlighted.
4Results
Binary
1011
Gray Code
1110
Decimal
11
Hexadecimal
0xB
Bit Length
4 bits
Formula Used
G = B ⊕ (B>>1)
How to Read It
The first bit is copied straight down. Each later Gray bit is the XOR of two adjacent binary bits.

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)

DecimalBinary (3-bit)Gray (3-bit)Binary (4-bit)Gray (4-bit)
000000000000000
100100100010001
201001100100011
301101000110010
410011001000110
510111101010111
611010101100101
711110001110100
810001100

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:

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.

Next step: Gray code orders the axes of every Karnaugh map — see it in action in the K-Map Solver, or explore the XOR gate behind the conversion in the Logic Gate 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.