Gray Code Converter

Convert between binary and Gray (reflected binary) code. Shows a table of sequential Gray codes for the chosen bit width, highlighting the single-bit change property.

Converter Electronics Updated Oct 16, 2026
How to Use
  1. Pick the bit width: 3, 4, 6 or 8 bits.
  2. Enter a decimal value to see its binary and Gray codes, or enter a Gray code to convert it back.
  3. Read the binary, Gray and decimal forms, and how many bits changed from the previous value (always one).
  4. Scroll the table to see the whole sequence for that width.
Input
Presets
Conversion
Decimal
—
Binary
—
Gray
—
Bits Changed
—

Worked Example

Convert 7 to 4-bit Gray code. In binary 7 is 0111. Shift right by one to get 0011, then XOR: 0111 ⊕ 0011 = 0100.

Back again: the first binary bit is the first Gray bit, 0; then 0 ⊕ 1 = 1, 1 ⊕ 0 = 1, 1 ⊕ 0 = 1, giving 0111 = 7. The next value, 8, is Gray 1100: only the top bit changes, while in binary all four bits flip from 0111 to 1000.

The common mistake: XORing with the wrong neighbour when converting back. Each binary bit is the previous binary bit XOR the current Gray bit, not the previous Gray bit; mixing them up gives wrong answers for most values.

Show Work: Sequential Table

Select bit width.

Formulas

Binary → Gray
G = B ⊕ (B >> 1)
Right-shift XOR; one instruction in most languages.
Gray → Binary
Bᵢ = ⊕(Gⱼ for j ≤ i)
Cumulative XOR from MSB down.
Property
Adjacent values differ by 1 bit
Defining feature of Gray code.
Reflected
Gray is a reflected binary code
Constructed by mirroring binary.
Cyclic
Last → First: 1 bit change
Standard Gray is cyclic.
Encoder Advantage
No transition ambiguity
No glitches when read mid-transition.

History of the Gray Code

Codes with the one-bit-change property were used before it had a name, including in Émile Baudot's telegraph equipment in the 1870s. Frank Gray, a physicist at Bell Labs, applied for a patent on the "reflected binary code" for pulse-code modulation in November 1947 (US 2,632,058, granted 1953), and the code has carried his name ever since.

The single-bit-change property became essential for rotary and linear position encoders. A sensor reading a binary-coded disk could catch the disk mid-transition and see a garbage value; a Gray-coded disk guarantees that any reading is either correct or off by one position, never wildly wrong. That is why absolute encoders commonly use Gray code, converting to binary with simple XOR logic where needed.

Modern applications extend beyond encoders. Karnaugh maps use Gray-code axis ordering so adjacent cells differ by one variable, simplifying logic minimization. Flash memory management uses Gray-coded multi-level cells to minimize bit error rates during read-disturb events. And multi-bit counters crossing asynchronous clock domains use Gray-coded bus encoding so synchronizer flip-flops only ever see one changing bit per cycle — dramatically reducing metastability risk.

About This Calculator

Enter either a decimal value or a Gray binary code, pick a bit width (3, 4, 6, or 8), and the tool converts in both directions. The sequential table shows every value for that width with only one bit changing from each row to the next — a visual demonstration of the defining Gray-code property.

Conversion formulas: Binary → Gray: G = B XOR (B >> 1) — one shift and one XOR. Gray → Binary: each binary bit is the XOR of all Gray bits from the MSB down to that position. Both are trivial in hardware and in most programming languages. Everything runs client-side; no values leave your browser.

Frequently Asked Questions

What is Gray code?

A way of counting in binary where each value differs from the next in exactly one bit. Reading a value while it changes can then be wrong by at most one step, never wildly wrong.

How do I convert binary to Gray?

XOR the number with itself shifted right by one: G = B ⊕ (B >> 1). For 7 (0111): 0111 ⊕ 0011 = 0100.

How do I convert Gray back to binary?

Work from the most significant bit: the first binary bit equals the first Gray bit, and each following binary bit is the previous binary bit XOR the next Gray bit. 0100 gives 0, 1, 1, 1 = 0111.

Why do encoders use it?

An absolute encoder disc read in plain binary can be caught mid-change between, say, 0111 and 1000, where all four bits flip and any mixture could be read. With Gray code only one bit changes, so the reading is either the old or the new position.

Why is it used for clock-domain crossing?

A counter passed between two clocks in Gray code changes one bit per step, so if the receiving side samples during a change it sees either the old or the new count, never a meaningless mixture.

How do I use the Gray Code Converter?

Simply type or paste your value and read the result, which refreshes the instant you change something. There is nothing to submit and nothing to wait for.

Do I need to install or sign up for anything?

Not at all — it runs in the browser with nothing to install and no account. After it loads once, it even works without an internet connection.

Is my information private?

Yes. Everything happens in your browser. Nothing you type is sent to a server or saved anywhere.

Common Use Cases

Absolute Rotary Encoders

Coded discs and magnetic encoders report Gray-coded positions so a reading taken mid-step is off by at most one count.

Karnaugh Maps

Rows and columns are ordered 00, 01, 11, 10 so neighbouring cells differ in one variable, which is what makes grouping work.

FIFO Pointers

Asynchronous FIFOs pass their read and write pointers between clock domains in Gray code.

Quadrature Signals

The A/B outputs of an incremental encoder step through 00, 01, 11, 10: a 2-bit Gray code.

Last updated: