ECParts Toolkit LogoECParts Toolkit

Gray Code Converter

Convert between binary, decimal indexes, and Binary-Reflected Gray Code, generate bounded Gray-code sequence windows, and analyze one-bit transitions for encoders and digital state systems.

DIG-007 is focused on BRGC conversion and state transition analysis. It does not simulate encoder bounce, metastability, CDC verification, hardware timing, arbitrary code families, CRC, line coding, or quadrature decoding.

Engineering tool

Gray Code Converter

Convert Binary-Reflected Gray Code, generate sequence windows, and analyze one-bit transitions for encoders and digital state systems.

Converter mode

Parameter panel

Binary input; spaces and underscores are allowed. Leading zeros are preserved by width.

Standard Gray-code word widths. Custom width uses the shared Digital Logic limit.

Result console

Gray Code
1110
Binary
1011
Gray
1110
Decimal Index
11
Hex Reference
0xB
Bit Width
4bits
Formula
G = B XOR (B >> 1)

Conversion steps

B        = 1011
B >> 1   = 0101
B XOR shifted = 1110

Formula reference

Binary-Reflected Gray Code Rules

BRGC maps an unsigned binary sequence index into a code word where adjacent sequence positions differ by one bit.

Binary to Gray: G = B XOR (B >> 1)Gray to Binary: B[n-1] = G[n-1]Gray to Binary: B[i] = B[i+1] XOR G[i]Sequence Gray(i) = i XOR (i >> 1)Difference mask: D = GrayA XOR GrayBHamming distance = number of set bits in D

Variable definitions

B
unsigned binary sequence index
G
Binary-Reflected Gray Code word
n
selected bit width
i
bit position or sequence index depending on formula context
Leading zeros preserve fixed-width encoder position
Decimal output is the decoded BRGC sequence index

Worked Examples

Binary 0000

0000 converts to Gray 0000.

Binary 0001

0001 converts to Gray 0001.

Binary 0010

0010 converts to Gray 0011.

Binary 0011

0011 converts to Gray 0010.

Binary 1011

1011 converts to Gray 1110.

Gray 1110

1110 decodes back to binary 1011.

Decimal 7

4-bit decimal 7 is binary 0111 and Gray 0100.

Gray 0100

4-bit Gray 0100 decodes to binary 0111, decimal 7.

3-bit Sequence

000, 001, 011, 010, 110, 111, 101, 100.

Binary Transition 3 to 4

Binary 011 to 100 changes three bits.

Gray Transition 3 to 4

BRGC 010 to 110 changes one bit.

Adjacent Pattern

0011 and 0010 have Hamming distance 1.

Same State

1010 and 1010 have Hamming distance 0.

Multiple Difference

0000 and 1111 have Hamming distance 4.

8-bit Boundary

Binary 11111111 converts to Gray 10000000.

64-bit Round Trip

A 64-bit binary word converts to Gray and back without 32-bit coercion.

3-bit Encoder Position Reference

3-bit absolute encoder Binary-Reflected Gray Code reference
PositionBinaryBRGC
0000000
1001001
2010011
3011010
4100110
5101111
6110101
7111100

Engineering Notes

  • This calculator implements Binary-Reflected Gray Code, not every possible Gray-code family.
  • Binary to Gray uses XOR between the binary word and a one-bit logical right shift of that word.
  • Gray to binary requires cumulative XOR and must not reuse the binary-to-Gray formula.
  • Leading zeros are part of the fixed-width code word and are preserved.
  • Gray code is treated as an unsigned state or sequence index, not a two's-complement signed number.
  • The decimal value shown is the decoded BRGC sequence index, not the raw Gray bits read as normal binary.
  • Gray code can reduce ambiguity caused by several bits changing at once.
  • Gray code does not eliminate metastability, encoder bounce, electrical noise, or poor signal integrity.
  • A one-bit Hamming distance does not by itself prove two code words are consecutive sequence positions.
  • For large widths, generate a sequence window rather than a full 2^n table.

Common Mistakes

  • Reading Gray bits directly as ordinary binary.
  • Using the binary-to-Gray formula to decode Gray back to binary.
  • Dropping leading zeros and losing position width.
  • Using ordinary JavaScript Number bitwise operators for 64-bit code words.
  • Assuming Gray code eliminates metastability.
  • Assuming every Gray-code family is BRGC.
  • Assuming Hamming distance 1 always means consecutive sequence indexes.
  • Trying to generate a complete 64-bit Gray sequence.
  • Mixing signed two's-complement interpretation into Gray-code conversion.
  • Confusing Gray code with error-correcting code.

Support reference

FAQ

What is Gray code?

Gray code is a class of codes designed so adjacent states typically differ by one bit. This calculator implements Binary-Reflected Gray Code.

How do I convert binary to Gray code?

Keep the most significant bit, then XOR each binary bit with the bit to its left. In value form, use G = B XOR (B >> 1).

How do I convert Gray code back to binary?

Use cumulative XOR from the most significant bit toward the least significant bit. Gray to binary is not the same formula as binary to Gray.

Why does Gray code change only one bit at a time?

Binary-Reflected Gray Code orders code words so consecutive sequence positions differ by one bit, reducing multi-bit transition ambiguity.

What is Binary-Reflected Gray Code?

Binary-Reflected Gray Code, or BRGC, is the most common engineering Gray-code construction and the one implemented by this converter.

Why is Gray code used in rotary encoders?

Absolute encoders can use Gray code so adjacent mechanical positions change one output bit instead of several bits at once.

How is Gray code used in asynchronous FIFOs?

FIFO pointers are often Gray-encoded before clock-domain crossing so only one pointer bit changes between adjacent count states.

Does Gray code prevent metastability?

No. Gray code can reduce multi-bit transition ambiguity, but CDC circuits still need synchronizers and proper timing methodology.

What is the Gray code for binary 1011?

For 4-bit Binary-Reflected Gray Code, binary 1011 converts to Gray 1110.

Can Gray code represent 64-bit values?

Yes. This converter uses BigInt-safe fixed-width logic so 64-bit Gray conversion can be handled without Number precision loss.

What is the difference between Gray code and binary?

Binary is a positional number representation. Gray code is an ordered code word system where adjacent sequence states are designed to change one bit.

Does a one-bit difference always mean two Gray codes are consecutive?

No. A Hamming distance of one only proves the code words differ by one bit. Sequence consecutiveness also depends on their decoded BRGC indexes.

Binary Arithmetic Calculator

Available

Calculate fixed-width binary arithmetic, carry, borrow, and overflow.

Open calculator

Two's Complement Calculator

Available

Convert signed two's-complement binary patterns and ranges.

Open calculator

Binary Bitwise Calculator

Available

Analyze masks, shifts, Hamming distance, and fixed-width bit operations.

Open calculator

Hex Decimal Binary Converter

Available

Convert basic number bases for firmware and register inspection.

Open calculator

Flip-Flop Calculator & Truth Table

Available

Analyze SR, JK, D and T sequential state behavior.

Open calculator

Counter & Modulus Calculator

Available

Calculate counter modulus, state capacity, unused states, and cascaded counter sizing.

Open calculator

Planned Guide

Gray Code Explained

Planned Guide

Binary vs Gray Code

Planned Guide

Gray Code in Rotary Encoders

Planned Guide

Gray Code in Asynchronous FIFOs

Planned Guide

Hamming Distance in Digital Systems