Free digital logic calculator

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.

Conversion direction
Enter only 0 and 1, optionally separated by spaces or underscores. Maximum input length: 4,000 bits, excluding separators.

Conversion result

Gray code output
Input

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
11Copy the first bit1
21 and 01 XOR 01
30 and 10 XOR 11
41 and 11 XOR 10

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:

The recovered binary value is 1011.

Binary and Gray code comparison table

Decimal value 4-bit binary 4-bit Gray code
000000000
100010001
200100011
300110010
401000110
501010111
601100101
701110100
810001100
910011101
1010101111
1110111110
1211001010
1311011011
1411101001
1511111000

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.

Leading zeros: This converter preserves the input width. For example, binary 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.