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
- Choose the conversion direction.
- Type the value.
- 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:
- Convert: Decimal → Gray
- Value: 7
- Bit width (padding): 4 bits
The calculator gives:
- Gray code: 0100
- Binary: 0111
- Decimal: 7
Tips and common mistakes
- Only one bit changes per step, so a misread encoder is off by at most one position.
- The first bit of Gray and binary is always the same.
- Gray code is not suitable for arithmetic — convert back first.
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
- Binary Calculator
- Number System Converter
- Logic Gate Calculator
- Two's Complement Calculator
- Parity Bit Calculator
- Hamming Code Calculator
- Truth Table Generator
- Karnaugh Map Solver
- Bitwise Calculator
- Bit Shift Calculator
Enter your values to get the result with the formula, substituted values and an explanation of each step.