Gray Code Converter

Gray code value (decimal)21
Input as binary1 1001
Result as Gray code1 0101
Bit width5 bits
How this was calculated
  1. A Gray code orders binary numbers so that neighbours differ in exactly one bit, which is why shaft encoders and error-tolerant counters use it. The conversion is pure XOR work on the binary form.
  2. Your input in binary is 1 1001. Encoding XORs the binary value with itself shifted one place right; decoding XORs the Gray code with shifted copies of itself until the binary value re-emerges.
  3. The result as Gray code is 1 0101, which is 21 in decimal. Check the defining property: it differs from the neighbouring code words in a single bit position.
  4. Because only one bit changes at a time, a sensor sampled mid-transition reads either the old or the new value - never a wild mixture - which is the whole point of the Gray code in position encoders and Karnaugh maps.

This gray code converter turns ordinary binary numbers into Gray code and turns Gray code back into binary. To encode, it applies a single XOR: Gray = n XOR (n shifted right by 1). To decode, it XORs the Gray code with shifted copies of itself until the binary value re-emerges. The results are exact, and each one comes with the steps that produced it. For example, the Gray code for 25 is 21: binary 1 1001 becomes Gray 1 0101. The Gray code for 42 is 63: binary 10 1010 becomes 11 1111. Going the other way, decoding Gray code 21 returns the binary value 25.

The method behind the converter is the accepted standard for this kind of calculation because it follows the definition of the code itself. Encoding is a single XOR, and decoding reverses that operation by XORing the Gray code with shifted copies of itself until the binary value re-emerges. The defining property is that neighbouring Gray code words differ in exactly one bit, which is why shaft encoders use the code. This page shows the formula, step-by-step workings, worked examples and sources, so you can check every result by hand rather than taking it on trust.

Worked examples

Encode 25

(Binary (decimal) to Gray code · 25)

Gray code value (decimal)21
Input as binary1 1001
Result as Gray code1 0101
Bit width5 bits

Encode 42

(Binary (decimal) to Gray code · 42)

Gray code value (decimal)63
Input as binary10 1010
Result as Gray code11 1111
Bit width6 bits

Decode 21

(Gray code to binary (decimal) · 21)

Binary value (decimal)25
Input as Gray code1 0101
Result as binary1 1001
Bit width5 bits

How binary converts to Gray code

Encoding a binary number as Gray code comes down to one operation: Gray = n XOR (n shifted right by 1). In plain terms, you take the binary value, slide a copy of it one place to the right, and exclusive-OR the two together. Each output bit is 1 only where the original bit and its right-hand neighbor differ, which is exactly the property that makes a gray code converter useful: the result changes by a single bit from one number to the next.

Walk through 25 first. In binary, 25 is 1 1001. Shift that right by one place and you get 0 1100. Line the two up and XOR column by column: 1 XOR 0 gives 1; 1 XOR 1 gives 0; 0 XOR 1 gives 1; 0 XOR 0 gives 0; and 1 XOR 0 gives 1. Reading the results left to right gives 1 0101, which is 21. So the Gray code for 25 is 21. Notice that the leading bit of the Gray word is always just copied from the leading bit of the binary number, because the shifted copy has a 0 in that position.

Now do 42. In binary, 42 is 10 1010. Shifted right by one place, it becomes 01 0101. XOR the two rows: 1 XOR 0 gives 1; 0 XOR 1 gives 1; 1 XOR 0 gives 1; 0 XOR 1 gives 1; 1 XOR 0 gives 1; and 0 XOR 1 gives 1. The result is 11 1111, which is 63. So the Gray code for 42 is 63. The two examples together show how the same one-line rule covers any width of binary input.

Decoding runs the process in reverse. Decoding XORs the Gray code with shifted copies of itself until the binary value re-emerges. As a check, decoding Gray code 21 returns the binary value 25, so the round trip holds. The payoff of this construction is that neighbouring Gray code words differ in exactly one bit, which is why shaft encoders use the code: at a boundary only one sensor changes state, so a misread can't produce a wildly wrong position.

  • 25: binary 1 1001, shifted 0 1100, XOR gives 1 0101 = Gray 21.
  • 42: binary 10 1010, shifted 01 0101, XOR gives 11 1111 = Gray 63.
  • Decoding Gray 21 returns binary 25.
The whole conversion is one XOR: Gray = n XOR (n shifted right by 1).

How Gray code converts back to binary

Decoding runs the encoding process in reverse, and the reverse of a single XOR is a short chain of XORs. The rule is simple: take the Gray code word and XOR it with a copy of itself shifted right by one bit, then repeat with the result, shifting right again each time, until every bit has been folded in. What comes out at the end is the binary value. A gray code converter does this folding in a loop, and because each pass shifts the working value one place to the right, the process always terminates: after as many passes as there are bits, the shifted copy is zero and nothing changes.

Follow the example of Gray code 21, which should decode to binary 25. Written in five bits, 21 is 1 0101. First pass: XOR 1 0101 with itself shifted right by one, giving 0 1010. XOR those two words bit by bit and the result is 1 1111. Second pass: XOR 1 1111 with its shifted copy 0 1111, which yields 1 0000. Third pass: XOR 1 0000 with 0 1000, giving 1 1000. Fourth pass: XOR 1 1000 with 0 1100, giving 1 0100. Fifth pass: XOR 1 0100 with 0 1010, giving 1 1001 — and 1 1001 is binary 25, the value the encoding started from.

Why does it converge? Each pass moves information one bit to the right, so the leftmost bit, which is already correct in Gray code and binary alike, stays fixed while the bits to its right are progressively corrected. Every pass settles at least one more bit, and once the shift has pushed all the original bits out, the working value stops changing. That is the convergence: the sequence of intermediate results is finite and ends at a fixed point, which is the binary answer. The same logic applies at any width — the Gray code for 42 is 63 (binary 10 1010 becomes 11 1111), and running the folding on 11 1111 walks back to 10 1010.

This is the mirror image of encoding, where the whole job is a single XOR: Gray equals n XOR n shifted right by one. Decoding looks like more work, but it is the same operation applied repeatedly, and the repeated XOR is what makes a gray code converter straightforward to build in software or hardware.

The reason the format is worth converting at all is that neighbouring Gray code words differ in exactly one bit, which is why shaft encoders use the code — a reading taken mid-transition can be off by only one position rather than several. Decoding XORs the Gray code with shifted copies of itself until the binary value re-emerges, restoring ordinary binary for arithmetic and display.

Decoding Gray code 21 returns the binary value 25 — the same 25 whose binary 1 1001 encodes to Gray 1 0101.

Why encoders use Gray code

A rotary encoder reports the position of a shaft as a number, and a linear encoder does the same for a sliding head. The tricky part is that the encoder reads its position while the shaft or slider is still moving. If the code it emits is plain binary, a change from one position to the next can flip several bits at once. During the instant those bits are changing, the sensor can catch a mix of old and new values, and that mix can spell a number that lies between the two real positions — or somewhere else entirely.

Gray code removes that risk because neighbouring code words differ in exactly one bit. That is the whole reason shaft encoders use the code. As the encoder advances one step, only a single line switches, so the worst a mid-transition sample can produce is either the position before the step or the position after it. There is no third, invented reading. For a controller tracking a moving shaft, that guarantee matters more than the convenience of binary numbering.

The single-bit property is easy to see in the numbers. Binary 1 1001 (25) becomes Gray 1 0101 (21), and binary 10 1010 (42) becomes Gray 11 1111 (63). Those encodings look arbitrary next to the binary originals, but each adjacent Gray word along the counting sequence changes in just one place, which is what the sensor needs. A gray code converter on this page lets you move between the two representations and check such pairs yourself.

The conversion itself is light enough to run inside encoder electronics. Encoding is a single XOR: Gray = n XOR (n shifted right by 1). Decoding works the other way, XORing the Gray code with shifted copies of itself until the binary value re-emerges; decoding Gray code 21 returns the binary value 25. Because the arithmetic is only shifts and XORs, it adds little cost to the readout path and no rounding error.

The same reasoning applies to linear scales, where the read head crosses many code tracks as it travels. Any position where several bits could change together is a position where a sample might be wrong, and Gray code eliminates those positions by construction. The result is a position reading that is always one of the two real neighbours, even when the sample lands in the middle of a transition.

One bit changes per step: a sample taken mid-transition can only return the position before or after the step, never a false in-between value.

Gray code beyond encoders

A gray code converter is usually reached for because of shaft encoders, but the one-bit property that makes the code useful in hardware also earns it a place in several other corners of engineering and computer science. Each of the following uses leans on the same fact: neighboring Gray code words differ in exactly one bit.

In digital logic design, Karnaugh maps are drawn so that adjacent cells differ by a single input variable, and that layout is built on Gray code ordering. When you label the rows and columns of a K-map in Gray sequence rather than plain binary, any two cells that touch share all but one bit, so a group of adjacent 1s can be simplified into a single product term. The map is, in effect, a Gray code converter drawn on paper: the ordering does the work of keeping logically adjacent states physically adjacent. Students who practice encoding and decoding by hand — for instance, seeing that the binary value 25 (1 1001) becomes Gray 1 0101, or that 42 (10 1010) becomes 11 1111 — tend to read K-map groupings more fluently, because they already see which cells are one bit apart.

Error-tolerant counters use the same idea. In a conventional binary counter, a step such as 0111 to 1000 flips four bits at once, and if those bits settle at slightly different times the counter can briefly output a spurious value. A counter that runs in Gray code changes only one bit per step, so there is no intermediate state to glitch through — the output is either the old word or the new one, never a mix. That property is why Gray-coded sequences appear in position feedback, FIFO pointer schemes, and other places where a single-bit transition makes the timing safe. The encoding itself is cheap: Gray = n XOR (n shifted right by 1), a single XOR per step.

Genetic algorithms borrow the code for a different reason. When candidate solutions are represented as bit strings and mutated, a plain binary encoding can make a one-bit flip cause a large jump in value — changing 0111 to 1000 moves the number by one, but other single flips move it much further. Gray encoding keeps small changes in the string close to small changes in the value, which can smooth the search landscape. The trade-off is that decoding costs more: you XOR the Gray code with shifted copies of itself until the binary value re-emerges, so decoding Gray 21 returns 25. Whether that extra work pays off depends on the problem, but the option is always available.

Neighboring Gray code words differ in exactly one bit — the property that makes the code useful in encoders also drives K-maps, glitch-free counters and Gray-encoded genetic algorithms.

FAQs

What is the Gray code for 25?

The Gray code for decimal 25 is 21. Start with the binary form of 25, which is 1 1001, then apply the binary-to-Gray conversion. The result is the Gray code 1 0101, which reads as 21 in decimal. So 25 in binary maps to 21 in Gray code.

How do I convert binary to Gray code?

XOR the number with itself shifted one place to the right. For example, take the number 42. After XORing it with its one-place right shift, the result is 63, which is the Gray code for 42. The same shift-and-XOR rule works for any binary value you convert.

How do I convert Gray code back to binary?

XOR the code with shifted copies of itself to recover the original binary value. As a check, Gray code 21 decodes to binary 25. That confirms the round trip: decimal 25 becomes Gray 21, and Gray 21 returns to binary 25. The converter applies this reverse process to give you the binary result.

Why is Gray code used in encoders?

In Gray code, neighbors differ in exactly one bit. So a sample taken mid-transition reads either the old value or the new value, never a wild mixture of bits. That single-bit change makes position encoders more reliable, since a poorly timed reading cannot produce a large, incorrect jump in the reported position.

What is the Gray code for 42?

The Gray code for decimal 42 is 63. Start with the binary form of 42, which is 10 1010, then convert it to Gray code. The result is 11 1111, which reads as 63 in decimal. So 42 in binary maps to 63 in Gray code.

Who invented Gray code?

Frank Gray of Bell Labs invented it. He patented the reflected binary code for pulse-code communication, and the converter on this page uses his construction. The name Gray code comes from him, and the reflected binary code is the same scheme commonly called Gray code today.

Sources

Related calculators

Binary Converter Calculator
Convert decimal numbers to binary, octal and hexadecimal — or read binary and octal numbers back into decimal — with the place-value working shown.
Two's Complement Calculator
Two's complement calculator: a decimal integer as an 8, 16 or 32-bit two's complement pattern, with hex and the unsigned reading.
Modulo Calculator
a mod n with the full division equation shown — including the negative-number case, where the mathematical (floor) answer and the JavaScript/C truncated remainder genuinely differ.