Skip to content

Decimal to Gray Code Converter

Convert decimal numbers to Gray code (reflected binary code) where consecutive values differ by only one bit. Essential for digital communications, encoders, and error correction

Category: data
Use Case: Digital Encoders, Error Correction, ADC/DAC
Privacy: 100% browser-based

Conversion Options

Common: 4, 8, 16 bits (0 = no padding)

Recommended Settings

Pro Tips

  • Gray code differs from binary by only one bit between consecutive values
  • Also called reflected binary code due to its mirror-like construction pattern
  • Only defined for non-negative integers (0 and positive numbers)
  • Formula: gray = decimal XOR (decimal >> 1)

Most Popular

Most users choose 4 or 8-bit padding to see the full Gray code sequence

When to Use This Tool

Rotary Encoders & Position Sensing

Design and debug rotary encoders, shaft encoders, and position sensors where Gray code eliminates reading errors during transitions. Since only one bit changes at a time, there's no ambiguity when reading mechanical positions, preventing false intermediate values.

Analog-to-Digital Converters (ADC)

Implement Gray code in ADC circuits to minimize errors during voltage transitions. Flash ADCs and successive approximation ADCs use Gray code to ensure that quantization errors affect only one bit at a time, improving conversion accuracy and reducing glitches.

Error Correction & Detection

Use Gray code in digital communication systems and error-correcting codes where single-bit errors are most common. The single-bit-change property makes it easier to detect and correct transmission errors, especially in noisy channels and wireless communications.

Genetic Algorithms & Optimization

Encode solutions in genetic algorithms using Gray code to ensure smooth fitness landscapes. Gray code representation reduces the impact of mutations since adjacent values differ by one bit, making the search space more continuous and improving convergence.

How It Works

1

Parse input decimal numbers (must be non-negative integers)

2

Convert decimal to binary representation using toString(2)

3

Apply Gray code formula: gray = binary XOR (binary >> 1)

4

Convert Gray code number back to binary string representation

5

Apply padding to specified bit length for consistent width

6

Group digits by specified size (typically 4-bit nibbles) if enabled

7

Optionally show side-by-side binary and Gray code comparison

100% Private

Files never leave your device. All processing happens locally in your browser.

Lightning Fast

Powered by Pure JavaScript for optimal performance on modern browsers.

Secure

No data collection, no tracking, no sign-up required.

Frequently Asked Questions

What is Gray code and why is it important?

Gray code (reflected binary code) is a binary numeral system where consecutive values differ by only one bit. Unlike standard binary where 3→4 changes three bits (011→100), Gray code changes only one bit (010→110). This single-bit-change property prevents reading errors in mechanical encoders and minimizes glitches in digital circuits.

How does Gray code prevent encoder reading errors?

In mechanical rotary encoders, multiple bits might not change simultaneously due to mechanical imperfections. Standard binary could read intermediate garbage values (e.g., during 3→4 transition, might briefly read 7). Gray code ensures only one bit changes, so even if read mid-transition, the value is either the old or new position - never a completely wrong value.

Can Gray code represent negative numbers?

No, Gray code is only defined for non-negative integers (0 and positive numbers). Unlike binary which has two's complement for negatives, Gray code doesn't have a standard negative representation. It's primarily used in contexts where only positive values or positions matter, like encoders and ADCs.

What's the formula for converting decimal to Gray code?

The formula is: gray = decimal XOR (decimal >> 1). In other words, XOR the number with itself right-shifted by one bit. For example, 6 in binary is 110. Right shift gives 011. XOR them: 110 XOR 011 = 101 (Gray code for 6).

Why is it called 'reflected' binary code?

Gray code is called reflected because the second half of any n-bit sequence mirrors the first half with the leading bit flipped. For 2-bit: 00, 01, 11, 10 - notice 01,00 is reflected as 11,10. This reflection property makes it easy to generate longer sequences from shorter ones.

Where else is Gray code used besides encoders?

Gray code is used in: analog-to-digital converters (ADCs) for smooth transitions, genetic algorithms for better mutation behavior, Karnaugh maps for digital logic simplification, error correction in communication systems, FPGA configuration to minimize power consumption during state changes, and solving puzzles like the Tower of Hanoi.