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 Apr 18, 2026
How to Use
  1. Enter either a decimal value or a Gray code directly.
  2. Bidirectional conversion: binary → Gray and Gray → binary.
  3. The sequential table shows all values for the chosen width with only one bit changing per step.
Input
Presets
Conversion
Decimal
Binary
Gray
Bits Changed

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

The "reflected binary code" was first described by the French engineer Émile Baudot (of baud-rate fame) in 1878 for use in telegraphy. Frank Gray, a physicist at Bell Labs, patented the code in 1947 for use in pulse-code modulation — the same era that produced Hamming\'s error-correcting codes and Shannon\'s information theory. Bell Labs patents granted Gray\'s name lasting association with the code despite its earlier provenance.

The single-bit-change property has been critical for rotary and linear position encoders since the 1960s. 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. This is why every modern industrial rotary encoder uses Gray code internally, converting to binary on the output side via simple XOR logic.

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 binary encoding where successive values differ by exactly one bit. Avoids race conditions when reading positional encoders — no intermediate invalid states.

How to convert?

Binary → Gray: G = B XOR (B >> 1). Gray → Binary: each binary bit = XOR of all Gray bits from MSB to that position.

Where is it used?

Rotary/linear encoders, absolute position sensors, Karnaugh maps, telegraphy, and any application where transitions must pass through valid states only.

Common Use Cases

Rotary Encoder

Hand-held optical encoder discs use Gray code so readings never show intermediate states during rotation.

K-maps

Karnaugh map column/row ordering uses Gray code so adjacent cells differ by one variable.

Clock Domain Crossing

Gray-coded counters reduce risk of metastability when passing multi-bit values between clock domains.

Last updated: