Binary to Gray Code Converter
Convert a binary number to Gray code instantly. Enter any binary bit pattern to generate the equivalent reflected binary Gray code and see the XOR calculation used for each output bit.
What Is a Binary to Gray Code Converter?
A binary to Gray code converter changes an ordinary binary bit pattern into the corresponding Gray code, also known as reflected binary code. Gray code is arranged so that two consecutive values differ by only one bit.
This one-bit-change property is the main reason Gray code is useful. In
ordinary binary counting, several bits can change at the same transition.
For example, moving from binary 0111 to 1000
changes all four bits. In Gray code, neighboring values are designed to
change only one position.
The conversion itself is simple. The most significant Gray bit is copied directly from the most significant binary bit. Every following Gray bit is produced by XORing two adjacent binary bits.
Binary code
Ordinary base-2 representation where bit patterns follow standard binary place-value rules.
Gray code
A unit-distance code where neighboring values differ in only one bit.
How to Use the Binary to Gray Code Converter
Type a value such as 1011. Only 0 and 1 are valid input
characters.
The converter generates the Gray code instantly once a valid binary pattern is entered.
The output contains the same number of bits as the original binary input.
For shorter values, BinaryCon shows how each Gray bit was generated from neighboring binary bits.
Use the copied Gray code in digital logic, encoder work, coursework, programming, or another calculation.
Binary to Gray Code Formula
Let the binary bits be written as
Bₙ Bₙ₋₁ ... B₁ B₀ and the corresponding Gray bits as
Gₙ Gₙ₋₁ ... G₁ G₀.
Gray[i] = Binary[i-1] XOR Binary[i]
XOR means “exclusive OR.” Its output is 1 when the two input bits are different and 0 when they are the same.
XOR Truth Table Used in Gray Code Conversion
| First Bit | Second Bit | XOR Result |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Worked Example: Convert Binary 1011 to Gray Code
Start with binary 1011. The Gray code output will also contain four bits.
Gray: 1 _ _ _ Step 2: XOR adjacent binary bits 1 XOR 0 = 1
0 XOR 1 = 1
1 XOR 1 = 0 Step 3: Write the Gray result Binary 1011 → Gray 1110 Therefore, 1011₂ converts to Gray code 1110.
Another Example: Binary 1111 to Gray Code
1 XOR 1 = 0
1 XOR 1 = 0 Final result: 1111 binary → 1000 Gray
Binary to Gray Code Reference Table
The table below shows the standard 4-bit reflected binary Gray code sequence. Notice that each adjacent Gray code differs from the previous code by exactly one bit.
| 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 Only One Bit at a Time?
Gray code is constructed as a unit-distance sequence. Adjacent values are deliberately arranged so that their bit patterns differ at only one position. This reduces ambiguity during transitions between neighboring states.
Consider ordinary binary counting from decimal 7 to 8:
8 decimal = 1000 binary Four binary bits change during this single count transition. In 4-bit Gray code: 7 decimal = 0100 Gray
8 decimal = 1100 Gray Only the leftmost bit changes.
In physical systems, individual signals may not change at exactly the same instant. Reducing a state transition to one changing bit can therefore reduce the chance of interpreting a temporary intermediate pattern incorrectly.
Where Gray Code Is Used
Rotary encoders
Position encoders can use Gray code so neighboring mechanical positions differ by only one encoded bit.
Digital electronics
Gray sequences can help reduce transition-related ambiguity in state representations and hardware interfaces.
Karnaugh maps
Karnaugh map rows and columns are arranged in Gray-code order so adjacent cells differ by only one Boolean variable.
Asynchronous FIFOs
Gray-coded pointers are commonly used when transferring counter state between different clock domains because only one pointer bit changes between adjacent counts.
Binary vs Gray Code
| Property | Binary | Gray Code |
|---|---|---|
| Primary purpose | Numeric representation | Unit-distance state representation |
| Place-value arithmetic | Yes | Not directly |
| Adjacent values | May change several bits | Change one bit |
| Common use | General computing | Encoders and digital systems |
Is Gray Code a Number System?
Gray code is better described as a binary encoding than a positional number system. An ordinary binary numeral has positional weights such as 1, 2, 4, 8, and 16. A Gray-code bit pattern does not have the same direct place-value interpretation.
To recover the corresponding ordinary binary value, Gray code must first be decoded. The first binary bit equals the first Gray bit, and each later binary bit is obtained from the previous binary bit XOR the current Gray bit.
1110
corresponds to binary 1011, not ordinary binary decimal 14.
Does Leading Zero Padding Affect Gray Code?
Bit width matters when representing Gray codes. Adding a leading zero to both the binary input and its fixed-width representation can produce a longer Gray code while representing the same non-negative binary magnitude.
For example:
Binary 01011 → Gray 01110
The second representation explicitly uses five bits. In digital hardware, keeping the intended bit width is often important, so this converter preserves every bit entered rather than stripping leading zeros.
Common Binary to Gray Code Mistakes
When converting binary to Gray, each new Gray bit is calculated from adjacent bits of the original binary input.
The most significant Gray bit is always identical to the most significant binary bit.
XOR outputs 0 for equal bits and 1 for different bits. Ordinary OR follows different rules and gives incorrect Gray code.
Gray code is an encoding. Its bit pattern must be decoded before applying ordinary binary place-value interpretation.
Related BinaryCon Tools
Continue working with binary representations, complements, logic, and number-system conversion using these related tools.
Binary to Gray Code Converter FAQs
How do you convert binary to Gray code?
Copy the most significant binary bit directly to the Gray output. Then, moving left to right, XOR each binary bit with the binary bit immediately before it. The XOR results form the remaining Gray code bits.
What is binary 1011 in Gray code?
Binary 1011 converts to Gray code 1110.
Copy the first 1, then calculate 1 XOR 0 = 1, 0 XOR 1 = 1, and
1 XOR 1 = 0.
What is binary 1111 in Gray code?
Binary 1111 becomes Gray code 1000.
The first bit stays 1. Every remaining adjacent pair is 1 XOR 1,
producing 0.
Why is the first Gray code bit the same as binary?
The standard reflected binary Gray conversion defines the most significant Gray bit as equal to the most significant binary bit. All remaining Gray bits describe whether neighboring binary bits differ.
What is XOR in Gray code conversion?
XOR means exclusive OR. For two bits, XOR returns 0 when both bits are equal and 1 when they differ. Thus 0 XOR 0 = 0, 0 XOR 1 = 1, 1 XOR 0 = 1, and 1 XOR 1 = 0.
Why is Gray code called reflected binary code?
One standard construction begins with a smaller Gray sequence, reflects that sequence in reverse order, prefixes one half with 0 and the reflected half with 1, and repeats the process to create larger Gray codes. This recursive construction gives reflected binary code its name.
Does Gray code have the same number of bits as binary?
Yes. Converting an n-bit binary pattern to reflected binary Gray code produces an n-bit Gray pattern. BinaryCon preserves leading zeros so the output width remains exactly the same as the entered width.
Can Gray code be converted back to binary?
Yes. Copy the first Gray bit to the first binary bit. Then each following binary bit is calculated as the previous decoded binary bit XOR the current Gray bit. This reverses the binary-to-Gray transformation.
Why is Gray code used in rotary encoders?
Neighboring positions can be assigned Gray patterns that differ by only one bit. If several physical sensors cannot switch at precisely the same instant, limiting a normal adjacent transition to one changing bit can reduce ambiguous intermediate readings.
Is Gray code useful in Karnaugh maps?
Yes. Karnaugh-map labels are arranged in Gray-code order so horizontally or vertically adjacent cells differ in only one Boolean variable. This adjacency is essential when grouping cells to simplify Boolean expressions.
Can this converter handle long binary bit strings?
Yes. Gray conversion is performed directly character by character using bit comparisons, so it does not require converting the entire input into a JavaScript floating-point number. Long bit patterns can therefore be processed without numeric rounding.
Are Gray code and binary numerically equal?
They represent the same indexed state when correctly encoded and decoded, but their visible bit strings are generally different. A Gray string should not be interpreted using ordinary binary positional weights until it has been converted back to binary.
Convert Binary to Gray Code Instantly
Use BinaryCon for fast Gray-code conversion, verified XOR calculations, reference sequences, digital-logic explanations, and free binary utilities whenever you need them.