New to Rust? Grab our free Rust for Beginners eBook Get it free →
Online Decimal to Gray Code Converter

For a nonnegative decimal integer, the next step is to find its matching Gray-code bit string. The online converter turns that integer into Gray code, a sequence of bits in which adjacent values differ by one bit.
I’ll show how to convert a value with the page and read the result as a Gray-code bit string.
TL;DR: Convert Decimal to Gray Code
Convert a nonnegative integer n to binary-reflected Gray code with the bitwise rule G = n XOR (n shifted right by one bit).
- Convert the decimal value to an integer, then apply the XOR rule.
- For 10, binary is 1010 and Gray code is 1111.
- Keep a fixed width when leading zero bits are part of a hardware interface.
What Is Decimal-to-Gray Conversion?
Gray code is an ordering of binary values where adjacent values differ by one bit.
A rotary sensor can sample a changing position. Binary 0111 to 1000 flips four bits, while the corresponding three-bit Gray values 100 and 110 differ by one bit.
For the standard binary-reflected form, convert the integer to binary and XOR it with a copy shifted right once. XOR, or exclusive OR, returns 1 when its two input bits differ and 0 when they match. For n = 10, binary 1010 XOR shifted value 0101 gives 1111, which is decimal 15 if interpreted as an ordinary binary integer.
I checked this formula against MathWorld and CP-Algorithms. Both references describe the same right-shift and XOR operation.
| Decimal | Binary | Gray code |
|---|---|---|
| 0 | 000 | 000 |
| 1 | 001 | 001 |
| 2 | 010 | 011 |
| 3 | 011 | 010 |
| 4 | 100 | 110 |
| 5 | 101 | 111 |
| 6 | 110 | 101 |
| 7 | 111 | 100 |
This table uses three-bit strings. A converter may omit leading zeros when it displays a minimal-width result.
This ordering is called reflected because each wider sequence is formed by taking the previous list in reverse and adding a leading 1 to the reversed half. Starting with 0 and 1, the two-bit sequence becomes 00, 01, 11, 10. This construction provides a useful visual check when you need several consecutive code values rather than a single conversion.
Reflection helps generate a complete lookup sequence for a chosen width. It also explains why Gray code is an ordering rather than ordinary binary arithmetic: moving to the next entry follows the sequence, not a place-value addition rule.
A code word is a bit string, but its ordinary binary integer value can differ. Decimal 2 has binary 10 and Gray code 11, which reads as 3 in base 2.
How to Convert Decimal to Gray Code Step by Step
Use the converter for a quick result or apply the same bit operation by hand. The walkthrough keeps the binary intermediate visible because it helps spot a wrong bit alignment.
Step 1: Enter a nonnegative integer
Type a whole decimal number such as 10 into the Decimal Input field. The current page accepts nonnegative integers written with decimal digits. Fractions, signs and letters fall outside that input format.
Step 2: Apply the right-shift and XOR rule
For 10, align binary 1010 with its one-bit right shift, 0101, then XOR the pairs to get 1111.
The leading bit remains unchanged because shifting inserts a zero at the left. Each following Gray bit records whether the corresponding binary bit differs from the bit immediately to its left.
Compare each aligned pair in 1010 and 0101 from left to right. Every pair differs, so each output bit is 1 and the result is 1111.
Step 3: Read the result as a bit string
Click Convert to Gray Code and read the output as a sequence of bits, not as a decimal answer. The converter labels its result as Gray Code Output. For input 10, the expected bit string is 1111.
Use a small value to check your arithmetic: 7 is binary 111, its right-shifted value is 011, and their XOR is 100. This agrees with the three-bit table. You can clear both fields before converting another value.
Step 4: Decode Gray code when you need the original value
To reverse the conversion, begin with the leftmost Gray bit as the first binary bit. For each next position, XOR the previous binary result with the next Gray bit. For Gray 1111, the binary bits are 1, 0, 1, 0, which is 10 in decimal.
Decoding is useful when a sensor reports a Gray word and the application needs an ordinary integer position. Read the input digits as a binary string.
When a decoded position seems wrong, verify that the bits arrived in the sensor’s specified order and that none were dropped by the interface.
A second check is Gray 101, which decodes to binary 110 and decimal 6.
Input Limits and Gray Code Checks
Check the input format and width before using a converted value in another system, because the implementation and receiving interface set practical limits.
| Case | What to expect | What to do |
|---|---|---|
| Zero | Gray code is 0 | Keep any required leading zeros for the receiving interface |
| Negative or fractional input | Outside the converter’s nonnegative-integer input | Use a documented signed or fixed-point encoding instead |
| Leading zero bits | They may not appear in a minimal-width result | Pad to the protocol or hardware width |
| Large integer | Browser bitwise operations can have finite integer limits | Verify the tool’s range before relying on large values |
The embedded tool passes JavaScript Number values to the right-shift and XOR operators. MDN documents that those operators coerce Number operands to signed 32-bit integers, so values above 2,147,483,647 can set the sign bit and larger values can lose high bits.
For wider inputs, use BigInt for both operands because mixing BigInt and Number throws a TypeError. See MDN’s right-shift reference and XOR reference.
A fixed-width unsigned field with k bits can represent 2^k values, from zero through 2^k minus one. Choose the width from the device or file format specification, then pad the Gray bit string. Padding changes display, not value.
For more context, see CodeForGeek’s free online developer tools and its Online Gray to Decimal Converter for the reverse direction.
Conclusion: Check the Binary Mapping
For a value from an encoder or logic exercise, label the result as Gray code and verify the decimal-to-binary-to-Gray mapping before using it. The references below document the forward and inverse rules.
For the formal definition and inverse operation, consult Wolfram MathWorld’s Gray Code reference and CP-Algorithms’ Gray code explanation.
FAQ
What is the formula for converting decimal to Gray code?
Convert the decimal integer to binary, then XOR it with the same value shifted right by one bit: G = n XOR (n >> 1).
Can Gray code represent negative numbers?
This converter accepts nonnegative integer input. A negative value needs a separately defined signed encoding and bit width.
Why can Gray code output have leading zeros?
A fixed-width representation may include leading zeros to preserve the number of bits required by an encoder or interface. A minimal-width display can omit them without changing the numeric bit pattern.
How do I convert Gray code back to decimal?
Recover the binary bits with cumulative XOR from the leftmost bit, then interpret that binary result as an ordinary integer.




