Gray Code
Gray code is an ordering of the binary number system such that two successive values differ in only 1-bit. They are useful in the normal sequence of the binary numbers generated by the hardware that can cause error, the gray code eliminates the problem as in this only 1-bit changes its value during transition from one number to another.
Construction of an n-bit gray code
It can be generated recursively using reflex and prefix method.
It can be explained as follows:
- Code is generated for n=1. code=0,1.
- Taking the previous code in sequence: 0,1.
- Then reverse the codes and add them to the sequenced code: 0,1,1,0.
- Then the prefix 0 is added to the previous codes and prefix 1 is added to the newer codes: 00,01,11,10.
For example, the gray code is as follows for the given n values:
For n=1-bit
Binary | Gray |
0 | 0 |
1 | 1 |
For n=2-bit
Binary | Gray |
00 | 00 |
01 | 01 |
10 | 11 |
11 | 10 |
For n=3-bit
Binary | Gray |
000 | 000 |
001 | 001 |
010 | 011 |
011 | 010 |
100 | 110 |
101 | 111 |
110 | 101 |
111 | 100 |
Reference