Understanding the Gray code converter — concept and principle
Gray code (reflected binary code) orders numbers so that consecutive values differ in exactly one bit. Ordinary binary does not have this property: going from 7 (0111) to 8 (1000) changes all four bits at once, and a sensor read during that transition can return any wrong value. Gray code is therefore used in rotary and linear position encoders, Karnaugh maps, genetic algorithms and counters that cross clock domains in digital hardware. This converter turns a decimal number into binary and Gray code, and also decodes the number's bits as if they were already Gray code.
How it is calculated
- Binary → Gray: G = n XOR (n >> 1). Each Gray bit is the XOR of a binary bit and the bit to its left; the top bit stays the same.
- Gray → binary: work from the top bit down, b(i) = g(i) XOR b(i+1). Equivalently, XOR the value with all of its right shifts: b = g ^ (g >> 1) ^ (g >> 2) ^ … until the shift reaches 0.
The principle
The reflected construction explains the one-bit property. Take the Gray list for k bits, write it again in reverse order underneath, then prefix the first half with 0 and the second half with 1. Inside each half neighbours already differ in one bit, and at the seam the two middle entries are identical apart from the new prefix bit. The XOR formula is a shortcut for this construction: a Gray bit is 1 exactly where the binary number changes from one bit to the next, so a step of +1, which flips a run of trailing bits in binary, changes only one of those "transitions".
Worked example
- Input: 13, binary 1101.
- Shift right by one: 13 >> 1 = 6 = 0110.
- Gray: 1101 XOR 0110 = 1011 (decimal 11).
- Now treat the input bits 1101 as Gray code: b3 = 1; b2 = 1 XOR 1 = 0; b1 = 0 XOR 0 = 0; b0 = 1 XOR 0 = 1, giving 1001.
- Check with shifts: 13 ^ 6 ^ 3 ^ 1 = 9, so the decoded value is 1001 (9).
| Decimal number | Binary |
|---|---|
| 3.25 | 11.01 |
| 6.5 | 110.1 |
| 9.75 | 1001.11 |
| 13 | 1101 |
| 16.2 | 10000.01 |
| 19.5 | 10011.1 |
| 26 | 11010 |
How to read the result
The Gray output is the bit pattern an encoder would show at position 13. The third line answers the opposite question: if a device reports the pattern 1101, the actual position is 9. Encoding then decoding always returns the original number, so Gray(13) = 1011 decodes back to 1101.
Common mistakes
- Using an arithmetic shift on negative numbers; Gray code is defined for non-negative integers.
- Decoding with a single XOR (g ^ (g >> 1)), which encodes again instead of decoding.
- Reading the Gray pattern as an ordinary binary number and comparing magnitudes directly.
Frequently asked questions
Why are rotary encoders Gray-coded?
Mechanical contacts never switch at exactly the same moment. With only one bit changing per step, a reading taken mid-transition is off by at most one position.
How are Gray codes used in Karnaugh maps?
Rows and columns are labelled in Gray order (00, 01, 11, 10) so that neighbouring cells differ in one variable, which makes groups easy to see.
Sources: F. Gray, Pulse Code Communication, U.S. Patent 2,632,058 (1953) · M. Morris Mano and Michael D. Ciletti, Digital Design, Pearson