Decimal to Gray Code Converter
Convert decimal numbers to Gray code online. Each decimal value is converted to binary, then XOR-shifted to produce its reflected Gray code.
Drop in decimal numbers, pick a bit width, and get back Gray code where each step changes only one bit. Handy for rotary encoders, Karnaugh maps, and any hardware where a mid-transition glitch on standard binary would read as a huge jump. And everything runs in your browser. Nothing gets uploaded.
How to Use
Enter decimal numbers in the left panel. One per line, or separated by spaces or commas. Click Sample for example values.
Choose bit padding (Auto, 4-bit, 8-bit, or 16-bit) and optionally enable Show binary intermediate to see the binary step.
Click Convert or leave Live mode enabled for real-time conversion as you type.
Copy, Download, or Swap the result. Swap converts Gray code values back to decimal numbers.
Frequently Asked Questions
What is Gray code?
Gray code (also called reflected binary code) is a binary numeral system where two successive values differ in only one bit. It was patented by Frank Gray in 1953. For example, in standard binary, going from 7 (0111) to 8 (1000) changes all four bits, but in Gray code, 7 is 0100 and 8 is 1100 -- only one bit changes. This single-bit-change property makes Gray code invaluable in reducing errors in digital systems.
How is Gray code calculated from decimal?
The conversion is a two-step process. First, convert the decimal number to its standard binary representation. Then, XOR the binary number with itself right-shifted by one position. In formula terms: Gray = Binary XOR (Binary >> 1). For example, decimal 5 is binary 101. Right-shift gives 010. XOR of 101 and 010 is 111, so the Gray code for 5 is 111.
What is the "Show binary intermediate" option?
When enabled, this option displays both the standard binary representation and the resulting Gray code for each decimal number. This is useful for learning and verification, as you can see the intermediate binary step before the XOR operation produces the final Gray code. Each output line will show the decimal, binary, and Gray code values.
What are common uses for Gray code?
Gray code is widely used in rotary encoders and position sensors, where it prevents spurious output during transitions. It is also used in Karnaugh maps for logic simplification, in error correction systems, in genetic algorithms, and in analog-to-digital converters. Any application where a single-bit transition error could cause a large value jump benefits from Gray code.
What does bit padding do?
Bit padding controls the minimum number of bits shown in the output. "Auto" uses the fewest bits needed to represent the largest value in the input. Choosing 4-bit, 8-bit, or 16-bit pads each Gray code value with leading zeros to the specified width. For example, decimal 3 in Gray code is 10, but with 8-bit padding it becomes 00000010. This is useful when you need fixed-width binary strings for hardware or protocol work.