Binary to Gray Code Converter
Convert binary numbers into reflected binary Gray code instantly. You can also decode Gray code back to binary, preserve leading zeros, and see each XOR operation explained step by step.
Gray Code Calculator
Choose the conversion direction, enter a bit string, and calculate the result. Spaces and underscores are optional.
Enter only 0 and 1, optionally separated by spaces or underscores. Maximum input length: 4,000 bits, excluding separators.Conversion result
Step-by-step calculation
What Is Gray Code?
Gray code, also called reflected binary code, is a binary encoding in which adjacent values in the standard sequence differ in exactly one bit. It is useful in digital systems where several bits changing at once can cause ambiguous intermediate readings.
Gray code is used in applications such as rotary position encoders, digital sensors, and some hardware state-transition designs. It is not ordinary positional binary, so a Gray code must be decoded before interpreting it as a conventional binary number.
Binary to Gray code formula
To convert binary to Gray code, copy the most significant bit unchanged. Each following Gray bit is the XOR of two adjacent bits in the original binary number.
Gray = Binary XOR (Binary shifted right by 1) G₀ = B₀; Gᵢ = Bᵢ₋₁ XOR BᵢHere, the bits are indexed from left to right, starting at zero. XOR produces 1 when its two input bits differ and 0 when they are equal.
Example: Convert binary 1011 to Gray code
| Position | Binary bit(s) | Operation | Gray bit |
|---|---|---|---|
| 1 | 1 | Copy the first bit | 1 |
| 2 | 1 and 0 | 1 XOR 0 | 1 |
| 3 | 0 and 1 | 0 XOR 1 | 1 |
| 4 | 1 and 1 | 1 XOR 1 | 0 |
Therefore, binary 1011 converts to Gray code
1110.
How to convert Gray code back to binary
Reverse conversion uses cumulative XOR. Copy the first Gray bit unchanged. For each subsequent position, XOR the previous binary result bit with the current Gray bit.
B₀ = G₀; Bᵢ = Bᵢ₋₁ XOR Gᵢ
For example, Gray code 1110 decodes as follows:
- First binary bit:
1 - Second binary bit:
1 XOR 1 = 0 - Third binary bit:
0 XOR 1 = 1 - Fourth binary bit:
1 XOR 0 = 1
The recovered binary value is 1011.
Binary and Gray code comparison table
| Decimal value | 4-bit binary | 4-bit 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 use Gray code instead of ordinary binary?
In ordinary binary counting, several bits can change at the same
time. For example, the transition from 0111 to
1000 changes all four bits. If a physical device reads
those bits during a transition, it may temporarily see an unintended
combination.
In the reflected Gray code sequence, consecutive values differ by one bit. This can reduce ambiguity during transitions in position encoders and similar systems. Gray code does not replace binary for general arithmetic; it is an alternative representation useful in particular digital applications.
0010 produces Gray code
0011, not the shortened string 11.
This is helpful when working with fixed-width digital values.
Frequently Asked Questions
How do I convert binary to Gray code?
Keep the first binary bit unchanged, then XOR every subsequent binary bit with the bit immediately to its left. The resulting bits form the Gray code.
What is the Gray code for binary 1011?
Binary 1011 converts to Gray code 1110.
The first bit stays the same, and the remaining bits are formed
by XORing adjacent binary bits.
How do I convert Gray code to binary?
Copy the first Gray bit. Then XOR each subsequent Gray bit with the previous binary result bit to reconstruct the binary number.
Why is Gray code called reflected binary code?
The standard sequence can be constructed by reflecting an existing sequence, prefixing 0 to the original half and 1 to the reflected half. This construction maintains the one-bit difference between consecutive entries.
Does Gray code only use 0 and 1?
Yes. Gray code is represented with binary digits, but its bit patterns follow a different encoding order from ordinary positional binary.
Does this converter support long binary strings?
Yes. It processes up to 4,000 input bits and preserves leading zeros. The calculation uses individual bits rather than converting the entire input to a JavaScript number, avoiding integer precision limitations.
Does Gray code conversion change the represented value?
Gray code is an encoding of the same underlying sequence index. To recover the conventional binary representation, decode the Gray code first. Treating Gray bits directly as an ordinary binary number can produce a different numerical value.
Related Binary and Digital Logic Calculators
Explore more conversion and arithmetic tools from Binarios.org.