TFT

Binary Gray Code Converter

Convert between binary and Gray code. This tool translates standard binary to reflected binary code (Gray code) and vice versa. Used in digital communications, encoders, and Karnaugh maps.

Binary to Gray Code Reference Table

DecimalBinaryGray Code
000000000
100010001
200100011
300110010
401000110
501010111
601100101
701110100
810001100
910011101
1010101111
1110111110
1211001010
1311011011
1411101001
1511111000

About Gray Code

Gray code (reflected binary code) is a binary numeral system where two consecutive values differ in only one bit position. This property makes it useful in digital communications, rotary encoders, and Karnaugh maps for minimizing errors during transitions.

Conversion: The MSB stays the same. Each subsequent bit is the XOR of the current and previous bit in the original number.

How It Works

This binary Gray code converter transforms between standard binary and Gray code (reflected binary code). Gray code ensures that consecutive values differ by only one bit, which is useful in many digital applications.

The conversion process:

  1. Binary to Gray: MSB stays the same. Each other bit = current binary bit XOR previous binary bit.
  2. Gray to Binary: MSB stays the same. Each other bit = current Gray bit XOR previous binary bit.
  3. XOR operation: The exclusive-OR operation is the key to both conversions.
  4. Step-by-step display: Shows each XOR operation for educational clarity.

For example: Binary 1011 → Gray 1110. Binary 0100 → Gray 0110. Notice consecutive Gray codes (0→1→2→3: 00→01→11→10) change only one bit at a time.

When You'd Actually Use This

Rotary Encoder Systems

Work with Gray code encoders that output position in Gray code to prevent reading errors during transitions.

Digital Communications

Use Gray code in modulation schemes (like QAM) to minimize bit errors from symbol misreading.

Karnaugh Map Design

Arrange K-maps using Gray code ordering so adjacent cells differ by one variable.

Error Minimization

Reduce errors in systems where values change incrementally and reading during transition matters.

Learning Digital Logic

Understand alternative binary encodings and their applications in digital systems.

CTF Challenges

Solve puzzles involving Gray code encoding and decoding in cybersecurity competitions.

What to Know Before Using

Gray code isn't for arithmetic

Don't try to add or multiply Gray codes directly. Convert to binary first, do math, convert back.

Multiple Gray code variants exist

This tool uses standard (reflected) Gray code. Other variants exist for specialized applications.

MSB is always the same

The leftmost bit doesn't change in conversion. It's the starting point for both algorithms.

Bit width must be consistent

Use the same number of bits for input and output. 4-bit binary converts to 4-bit Gray code.

Gray code wraps around

The last and first Gray codes also differ by one bit. This cyclic property is useful in some applications.

Common Questions

Why is it called Gray code?

Named after Frank Gray, a Bell Labs researcher who patented the reflected binary code in 1953 for shaft encoder applications.

What's the main advantage of Gray code?

Consecutive values differ by exactly one bit. This prevents errors when reading values during transitions - you get either the old or new value, never garbage.

How do rotary encoders use Gray code?

As the shaft rotates, the output changes one bit at a time. Even if read mid-transition, you get a valid adjacent value, not a random number.

What's the Gray code sequence for 0-15?

0000, 0001, 0011, 0010, 0110, 0111, 0101, 0100, 1100, 1101, 1111, 1110, 1010, 1011, 1001, 1000. Notice each changes one bit.

Can I do math directly in Gray code?

Not practically. Convert to binary, do arithmetic, convert back. Gray code is for representation and transmission, not computation.

Where else is Gray code used?

Digital TV (QAM modulation), genetic algorithms, Karnaugh maps, analog-to-digital converters, and anywhere single-bit transitions matter.

What's the XOR formula for conversion?

Binary to Gray: G[i] = B[i] XOR B[i+1]. Gray to Binary: B[i] = B[i+1] XOR G[i]. Work from MSB to LSB.