Gray Code

Gray Code

Published by: Nuru

Published date: 22 Jun 2021

Gray Code Photo

Gray Code

Gray Code was named after Frank Gray. It is an ordering of binary number systems such that two successive values differ in only one bit. They are very useful in the normal sequence of binary numbers generated by the hardware that may cause an error or ambiguity during the transition from one number to the next. So, it can eliminate this problem easily since only one bit changes its value during any transition between two numbers.  It is also used as a solution guide for the tower of Hanoi problem, used in labeling the axes of K-maps, and allows the system designers to perform error checking such as digital terrestrial television and some cable TV system.

Gray Code is also known as reflected code. It is used in applications where the normal sequence of binary numbers may produce an error or ambiguity during the transition from one number to the next.

It is not weighted which means it does not depend on the positional value of the digit. This cyclic variable code means every transition from one value to the next value involves only one-bit change.

Constructing an n-bit Gray code:

n-bit Gray code can be generated recursively using the reflect and prefix method which is explained as following below.

  • Generate code for n=1: 0 and 1 code.
  • Take the previous code in sequence: 0 and 1.
  • Add reversed codes in the following list: 0, 1, 1, and 0.
  • Now add prefix 0 for the original previous code and prefix 1 for newly generated code: 00, 01, 11, and 10.

Therefore, 0 and 1 are for Binary numbers 0 and 1 respectively. Gray codes: 00. 01, 11, and 10 are for Binary numbers: 00, 01, 10, and 11 respectively. Similarly, you can construct Gray code for 3-bit binary numbers:

Grey Code

 

Conversion from BCD to Gray

  • MSB of the gray code and binary code is the same.
  • Going from left to right, add each adjacent pair of binary code bits to get the next gray code bit discarding the carry obtained.
Input Output
0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 1
0 0 1 0 0 0 1 1
0 0 1 1 0 0 1 0
0 1 0 0 0 1 1 0
0 1 0 1 0 1 1 1
0 1 1 0 0 1 0 1
0 1 1 1 0 1 0 0
1 0 0 0 1 1 0 0
1 0 0 1 1 1 0 1

Fig: Truth table for BCD to gray conversion

Binary to Gray Code Conversion

  • The left-most bit in the gray code is the same as the corresponding MSB in the binary number.
  • Going from left to right, add each pair of binary code bits to get the next Gray code bit. Discard carries.

Gray to Binary Conversion

  • The left-most bit in the binary code is the same as the corresponding bit in the reflected code.
  • Add each binary code bit generated to the reflected code bit in the next adjacent position. Discard carries.