Gray Code Converter Calculator

Convert binary or decimal to reflected Gray code and back, with the full Gray sequence.

What it does

In Gray code, consecutive numbers differ in exactly one bit. Convert binary or decimal to Gray code and back.

Where it's used

Rotary encoders, K-map ordering and safe counters crossing clock domains.

How to use it

  1. Choose the conversion direction.
  2. Type the value.
  3. Set the bit width for padding.

The formula explained

G
B ⊕ (B ≫ 1) — binary to Gray
B
G ⊕ (G ≫ 1) ⊕ (G ≫ 2) ⊕ … — Gray to binary

Worked example

Decimal 7 in 4-bit Gray code.

Inputs:

The calculator gives:

Tips and common mistakes

Questions people ask

How do I convert binary to Gray code?

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

How do I convert Gray code back to binary?

Keep the first bit; then each binary bit is the previous binary bit XOR the next Gray bit. 1110 in Gray gives 1, 0, 1, 1 — binary 1011, which is 11.

Where is Gray code used?

In rotary and linear position encoders, on the axes of Karnaugh maps, and for FIFO pointers that cross between two clock domains, where only one bit may change at a time.

Related Digital Logic calculators

Enter your values to get the result with the formula, substituted values and an explanation of each step.

All engineering calculators