A Gray code lists binary patterns so that each step changes exactly one bit. The standard 2-bit sequence is 00, 01, 11, 10, and the 3-bit one is 000, 001, 011, 010, 110, 111, 101, 100. In both, the jump from the last pattern back to the first also changes just one bit, so the sequence is cyclic.
Compare ordinary binary: going from 01 to 10 flips two bits at once, and 011 to 100 flips three.
Building it by reflection. Take the (n−1)-bit list, write it forwards with a 0 in front, then backwards with a 1 in front. From 0, 1 you get 00, 01, 11, 10. Repeat to get the 3-bit list.
Converting binary to Gray. Keep the most significant bit. Each other Gray bit is the XOR of a binary bit and the binary bit to its left. In the table above, G2 = B2, G1 = , G0 = .
Converting Gray to binary. Keep the MSB. Each next binary bit is the XOR of the binary bit you just found and the next Gray bit.
Why it matters here. A karnaugh map labels its axes in Gray order, so touching cells differ in one variable and can be grouped. Gray codes are also used in rotary position sensors and in counters that cross clock domains, where changing only one bit at a time avoids reading a half-changed value.
| G2 | G1 | G0 | |||
|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 | 0 | 1 |
| 0 | 1 | 0 | 0 | 1 | 1 |
| 0 | 1 | 1 | 0 | 1 | 0 |
| 1 | 0 | 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 1 | 1 | 1 |
| 1 | 1 | 0 | 1 | 0 | 1 |
| 1 | 1 | 1 | 1 | 0 | 0 |