Binary to Gray Code Converter

Convert between binary and Gray code instantly. Free online bidirectional converter for digital electronics and computer science.

Enter up to 32 bits (only 0s and 1s)

Binary to Gray Code Quick Reference

Decimal Binary Gray Code Decimal Binary Gray Code
000000000810001100
100010001910011101
2001000111010101111
3001100101110111110
4010001101211001010
5010101111311011011
6011001011411101001
7011101001511111000

Understanding Gray Code

Gray code, also known as reflected binary code (RBC) or reflected binary (RB), is a binary numeral system where two successive values differ in only one bit position. This unique property, called the "unit distance" property, makes Gray code extremely valuable in digital systems where errors must be minimized during state transitions.

Unlike standard binary representation where incrementing a number can change multiple bits simultaneously (e.g., 0111 to 1000 changes all four bits), Gray code ensures only one bit changes at a time. This characteristic prevents race conditions and reduces errors in mechanical and electrical systems.

Conversion Algorithms

Binary to Gray Code

Algorithm:

  1. Copy the MSB (leftmost bit) as is
  2. For each remaining bit: XOR with the previous binary bit
  3. Result is the Gray code

Example: 1011 → Gray

Binary: 1 0 1 1

Step 1: 1 (MSB stays same)

Step 2: 1⊕0 = 1

Step 3: 0⊕1 = 1

Step 4: 1⊕1 = 0

Gray: 1110

Gray Code to Binary

Algorithm:

  1. Copy the MSB (leftmost bit) as is
  2. For each remaining bit: XOR with the previous binary bit
  3. Result is the binary code

Example: 1110 → Binary

Gray: 1 1 1 0

Step 1: 1 (MSB stays same)

Step 2: 1⊕1 = 0

Step 3: 0⊕1 = 1

Step 4: 1⊕0 = 1

Binary: 1011

Real-World Applications of Gray Code

Rotary Encoders

Shaft position sensors use Gray code to prevent incorrect readings during transitions. Since only one bit changes at a time, mechanical imperfections don't cause multiple bits to flip simultaneously, ensuring accurate position detection.

Karnaugh Maps

K-maps use Gray code ordering for their rows and columns. This arrangement ensures adjacent cells differ by only one variable, making it easier to identify and simplify Boolean expressions in digital logic design.

Error Correction

Digital communication systems use Gray code to minimize errors. When noise causes a bit error, it's more likely to result in a value that's only off by one count rather than multiple counts as with binary.

Analog-to-Digital Converters

Some ADCs use Gray code internally because it reduces errors when the analog input is near a transition point between two digital values, improving conversion accuracy.

Genetic Algorithms

Gray code is used in genetic algorithms because single-bit mutations in Gray code result in small changes in the represented value, making the search space more continuous and improving convergence.

Position Tracking

Absolute position encoders in robotics and CNC machines use Gray code to track position accurately without cumulative errors, even during power cycles or system resets.

Frequently Asked Questions

What is Gray code?

Gray code (also known as reflected binary code) is a binary numeral system where two successive values differ in only one bit. This property makes it useful in error correction, digital communications, and mechanical encoders where minimizing errors during transitions is important.

Why use Gray code instead of binary?

Gray code is used in applications where only one bit should change at a time to prevent errors. For example, in rotary encoders, using Gray code ensures that mechanical imperfections don't cause multiple bits to change simultaneously, which would create incorrect readings. It's also used in K-maps for simplifying Boolean algebra.

How does binary to Gray code conversion work?

To convert binary to Gray code: The most significant bit (MSB) stays the same. Each subsequent bit is the XOR of the current binary bit and the previous binary bit. For example: Binary 1011 becomes Gray 1110.

How does Gray code to binary conversion work?

To convert Gray code to binary: The MSB stays the same. Each subsequent bit is the XOR of the current Gray bit and the previously calculated binary bit. This process reconstructs the original binary number from its Gray code representation.

Where is Gray code used in real applications?

Gray code is used in rotary encoders (shaft position sensors), error correction in digital communications, Karnaugh maps (K-maps) for Boolean logic simplification, analog-to-digital converters (ADC), genetic algorithms, and any system where minimizing errors during state transitions is critical.