EN

Gray code

Binary ↔ Gray converter, n-bit table and absolute encoder disc

Number systems: Binary and hexadecimal Gray code BCD codes Floating and fixed point Parity, Hamming and CRC

Binary to Gray and Gray to binary converter

Type in any box. With the −1 and +1 buttons you can see how many bits change when moving to the next value in binary and in Gray.

Value

Step by step

Gray code table and encoder disc

In the Gray column, the only bit that changes from the previous row is in bold. Click a row to send that value to the converter and the discs.

On the discs, the inner track is the most significant bit and the outer one the least significant; a black sector is a 1. The red line marks the position being read. On the binary disc there are positions where several tracks change at once; on the Gray disc, always just one.

What Gray code is

In Gray code, also called reflected binary, two consecutive values differ in only one bit. In natural binary, going from 7 (0111) to 8 (1000) changes all four bits, and if they do not change at exactly the same moment, false values are read for an instant: 0111 → 1111 → 1011 → 1000, that is, 7 → 15 → 11 → 8. With Gray (0100 → 1100) that cannot happen: the value read is either the previous one or the next, never a distant one. It is also cyclic: the last code also differs from the first in a single bit.

Gray is not a weighted code: a bit is not always worth the same, so it has to be converted to binary before doing arithmetic.

How it is built: the reflection

The n-bit table comes from the (n − 1)-bit one: write the list, underneath write the same list backwards (reflected as in a mirror) and put a 0 in front of the first half and a 1 in front of the second. From 1 bit (0, 1) you get 2 bits (00, 01, 11, 10), and from there 3 bits:

3-bit Gray

0 00 · 0 01 · 0 11 · 0 10  |  1 10 · 1 11 · 1 01 · 1 00

That is: 000, 001, 011, 010, 110, 111, 101, 100. At the mirror line (010 → 110) only the added bit changes.

Conversion formulas

\[ \text{Binary} \to \text{Gray: } g = b \oplus (b \gg 1) \qquad \text{Gray} \to \text{binary: } b_{k} = g_{k},\; b_{i} = b_{i+1} \oplus g_{i} \]

To convert to Gray, XOR each bit with the one on its left; the most significant bit is copied as it is. To go back, copy the first bit, and each following bit is the XOR of the previous binary bit with the Gray bit: \(1101_{Gray} \to 1,\;1\oplus1=0,\;0\oplus0=0,\;0\oplus1=1 \to 1001_2 = 9\). In a PLC it is done with a loop of XORs and shifts, or with a lookup table if there are few bits; in a circuit, with a chain of XOR gates.

Where it is used

Encoders without 2ⁿ positions: excess Gray

A 360-position encoder needs 9 bits (512 codes). If codes 0 to 359 were used, several bits would change going from 359 to 0. The solution uses the symmetry of the reflection: the central codes are used, from 76 to 435 (leaving \((512 - 360)/2 = 76\) on each side). Codes 435 and 76 are mirror images of each other and differ only in the most significant bit (\(101101010\) and \(001101010\)), so the full turn is still a one-bit change. When reading, the code is converted to binary and 76 is subtracted. That is why some encoders state an offset or excess in their data sheet.

Other unit-distance codes

Related: binary and hexadecimal converter, pulse counting inputs and parity, Hamming and CRC.