Decimal to Gray Code Converter
Convert a non-negative decimal integer to reflected binary Gray code. See the ordinary binary representation, right-shifted binary value, XOR operation, Gray code result, and step-by-step conversion.
What Is Decimal to Gray Code Conversion?
Decimal to Gray code conversion changes an ordinary base-10 integer into its reflected binary Gray code representation. Gray code uses only 0 and 1, but its ordering differs from ordinary binary.
A useful property of reflected Gray code is that adjacent values differ by only one bit.
Decimal to Gray Code Formula
For a non-negative integer N, its Gray code value can be calculated
with:
How to Convert Decimal to Gray Code
1. Start with decimal
Enter the non-negative decimal integer you want to encode.
2. Convert it to binary
Find the ordinary base-2 representation of the decimal number.
3. Shift binary right
Create a second value by shifting the original binary number one position right.
4. Align both values
Keep the shifted value aligned with the original bit positions.
5. XOR the bits
Apply XOR between the original binary number and its right-shifted version.
6. Read the Gray code
The XOR result is the reflected binary Gray code.
Example: Convert Decimal 10 to Gray Code
Example: Convert Decimal 5 to Gray Code
Decimal to Gray Code Table
| Decimal | Binary | Gray Code |
|---|---|---|
| 0 | 0000 | 0000 |
| 1 | 0001 | 0001 |
| 2 | 0010 | 0011 |
| 3 | 0011 | 0010 |
| 4 | 0100 | 0110 |
| 5 | 0101 | 0111 |
| 6 | 0110 | 0101 |
| 7 | 0111 | 0100 |
| 8 | 1000 | 1100 |
| 9 | 1001 | 1101 |
| 10 | 1010 | 1111 |
| 11 | 1011 | 1110 |
| 12 | 1100 | 1010 |
| 13 | 1101 | 1011 |
| 14 | 1110 | 1001 |
| 15 | 1111 | 1000 |
Why Does Gray Code Change One Bit at a Time?
In the standard reflected Gray-code sequence, consecutive encoded integers
differ in exactly one bit position. For example, decimal 2 has Gray code
0011, while decimal 3 has Gray code 0010.
Only the final bit changes between those two Gray values. This property is useful when several binary signals cannot be guaranteed to switch at exactly the same instant.
Decimal, Binary and Gray Code
| System | Purpose | Example for Decimal 10 |
|---|---|---|
| Decimal | Base-10 numeric notation | 10 |
| Binary | Base-2 numeric representation | 1010 |
| Gray Code | Single-bit-transition encoding | 1111 |
Why Use XOR for Decimal to Gray Conversion?
The expression N XOR (N >> 1) compares each binary bit with its
neighboring higher-order bit. XOR produces 1 when those aligned bits differ and
0 when they are the same.
That operation directly creates the reflected Gray-code representation of the integer.
Where Is Gray Code Used?
Gray code is commonly discussed in digital electronics, rotary and linear encoders, Karnaugh maps, hardware counters, position sensing, and systems where reducing ambiguity during state transitions is useful.
It is an encoding scheme rather than a replacement for ordinary binary arithmetic.
Common Decimal to Gray Code Mistakes
Reading binary as Gray code
Binary and Gray code both contain 0 and 1, but their encoded values are not generally identical.
Using OR instead of XOR
Standard binary-reflected Gray conversion specifically requires XOR.
Shifting in the wrong direction
The formula uses the original number shifted one bit to the right.
Using decimal digits directly
Gray encoding is derived from the binary representation of the decimal integer.
Related BinaryCon Tools
Decimal to Gray Code Converter FAQs
How do you convert decimal to Gray code?
What is the formula for decimal to Gray code?
Gray = N XOR (N >> 1).
What is decimal 2 in Gray code?
10. Its standard Gray code is 11.
What is decimal 5 in Gray code?
101. XORing it with 010 gives
Gray code 111.
What is decimal 10 in Gray code?
1010. XOR with 0101 gives
Gray code 1111.
What is decimal 0 in Gray code?
0.