Gray coding

HomePage | Recent changes | View source | Discuss this page | Page history | Log in |

Printable version | Disclaimers | Privacy policy

A gray code is a special coding system designed to prevent spurious output from practical electromechanical switches. For example, if we create a device that indicates position by closing and opening switches, we could use the binary code to represent position:

Binary Code
Input position Output 3 Output 2 Output 1
0 0 0 0
1 0 0 1
2 0 1 0
3 0 1 1
4 1 0 0
5 1 0 1
6 1 1 0
7 1 1 1

The problem is that, with real (mechanical) switches, it is very unlikely that two switches will change states exactly in synchrony. For example, in the transistion from state 3 to state 4, all three switches change state. In the brief period while all are changing, the switches will read some spurious position.

A gray code solves this problem by changing only one switch at a time, so there is never any ambiguity of position:

Gray Code
Input position Output 3 Output 2 Output 1
0 0 0 0
1 0 0 1
2 0 1 1
3 0 1 0
4 1 1 0
5 1 1 1
6 1 0 1
7 1 0 0

Notice also that state seven can roll over to state zero with only one switch change.