Gray Code: Binary to Gray Code Converter

What is Gray Code?

Gray code – also known as Cyclic Code, Reflected Binary Code (RBC), Reflected Binary (RB) or Grey code – is defined as an ordering of the binary number system such that each incremental value can only differ by one bit. In gray code, while traversing from one step to another step only one bit in the code group changes. That is to say that two adjacent code numbers differ from each other by only one bit.

Gray code is the most popular of the unit distance codes, but it is not suitable for arithmetic operations. Gray code has some applications in analog to digital converters, as well as being used for error correction in digital communication. Gray code can be difficult to understand initially, but becomes much easier to understand when looking at the gray code tables below.

Gray Code Table

A table showing the conversion between binary code to gray code and decimal to gray code is shown below:

Gray Code Table

Binary to Gray Code Converter

The logical circuit which converts the binary code to equivalent gray code is known as binary to gray code converter. An n-bit gray code can be obtained by reflecting an n-1 bit code about an axis after 2n-1 rows and putting the MSB (Most Significant Bit) of 0 above the axis and the MSB of 1 below the axis. Reflection of Gray codes is shown below.

The 4 bit binary to gray code conversion table is given below:

Binary To Gray Code Converter

That means, in 4-bit gray code, (4-1) or 3-bit code is reflected against the axis drawn after the (24-1)th or 8th row.

The binary to gray code converter circuit is shown below:

BCD to Gray Code

How to Convert Binary to Gray Code

  1. The MSB (Most Significant Bit) of the gray code will be exactly equal to the first bit of the given binary number.
  2. The second bit of the code will be exclusive-or (XOR) of the first and second bit of the given binary number, i.e if both the bits are same the result will be 0 and if they are different the result will be 1.
  3. The third bit of gray code will be equal to the exclusive-or (XOR) of the second and third bit of the given binary number. Thus the binary to gray code conversion goes on. An example is given below to illustrate these steps.

Binary to Gray Code Conversion Example

Say we have a binary number 01001 which we wish to convert to gray code. Let’s go through an example of how we would perform this conversion:

  1. The MSB is kept the same. As the MSB of the binary is 0, the MSB of the gray code will be 0 as well (first gray bit)
  2. Next, take the XOR of the first and the second binary bit. The first bit is 0, and the second bit is 1. The bits are different so the resultant gray bit will be 1 (second gray bit)
  3. Next, take the XOR of the second and third binary bit. The second bit is 1, and the third bit is 0. These bits are again different so the resultant gray bit will be 1 (third gray bit)
  4. Next, take the XOR of third and fourth binary bit. The third bit is 0, and the fourth bit is 0. As these are the same, the resultant gray bit will be 0 (fourth gray bit)
  5. Lastly, take the XOR of the fourth and fifth binary bit. The fourth bit is 0, and the fifth bit is 1. These bits are different so the resultant gray bit will be 1 (fifth gray bit)
  6. Hence the result of binary to gray code conversion of 01001 is complete, and the equivalent gray code is 01101.
Gray Code Converter

Gray to Binary Code Converter

In a gray to binary code converter, the input is gray code and output is its equivalent binary code.

Let us consider a 4 bit gray to binary code converter. To design a 4 bit gray to binary code converter, we first have to draw a gray code conversion table, as shown below:

Binary to Gray Code Truth Table

The gray code to binary converter circuit is shown below:

Gray To Binary Code Converter Circuit

Gray Code to Binary Conversion

Gray code to binary conversion is again a very simple and easy process. Following steps can make your idea clear on this type of conversions.

  1. The MSB of the binary number will be equal to the MSB of the given gray code.
  2. Now if the second gray bit is 0, then the second binary bit will be the same as the previous or the first bit. If the gray bit is 1 the second binary bit will alter. If it was 1 it will be 0 and if it was 0 it will be 1.
  3. This step is continued for all the bits to do Gray code to binary conversion.
Gray Code To Binary

One example given below will make your idea clear.

Gray Code to Binary Conversion Example

The MSB of the binary will be 0 as the MSB of gray is 0. Now move to the next gray bit. As it is 1 the previous binary bit will alter i.e it will be 1, thus the second binary bit will be 1. Next look at the third bit of the gray code. It is again 1 thus the previous bit i.e the second binary bit will again alter and the third bit of the binary number will be 0. Now, the 4th bit of the given gray is 0 so the previous binary bit will be unchanged, i.e 4th binary bit will be 0. Now again the 5th grey bit is 1 thus the previous binary bit will alter, it will be 1 from 0. Therefore the equivalent binary number in case of gray code to the binary conversion will be (01001).

Binary To Grey
Binary To Grey Converter
4 bit binary to gray code converter
BCD To Grey Code
Grey To Binary Conversion
Binary To Grey Formula

Gray Code Example Sequence

  1. Gray code can only change one bit from its previous step to its next step. The change of bit always occurs from the right side to the left side i.e. from LSB (Least Significant Bit) towards the MSB (Most Significant Bit).
  2. We know that for binary digit there are two possible combinations – 0 and 1. Following our rule, we first target the LSB (right side). Hence the first three bits are kept constant (i.e. 000) and the fourth bit changes from 0 to 1. This is the decimal equivalent of 1.
  3. Now move to the next bit from the LSB i.e. the 3rd bit. We now change this bit from 0 to 1 which is the decimal equivalent for 2 (0011). Note that unlike binary, we can not go from 0001 to 0010, as this is changing both the 3rd bit (0 to 1) and the 4th bit (1 to 0) at the same time.
  4. Now to increment from 0011, we start at the LSB (right side) and note that we have not seen the combination 0010 so far. As such, we keep the first three bits constant and change the last bit from 1 to 0. Hence the gray code for decimal number 3 is 0010
  5. Traverse to the next code. We can not change the 3rd or 4th bit as we have seen these combinations before. Hence we move on to the second bit and change that from a 0 to a 1. This gives the decimal number 4 as 0110 in gray code. You may question why we can’t we change the third bit again which will also be a one-bit change from the previous. But as changing the third bit would give the equivalent gray code 0000 which has occurred earlier (decimal number 0), we can not do this. Remember that a number occurring previously cannot be repeated.
  6. Repeating our previous steps, we will keep the first and second bit constant and find the possible combinations of the third and the fourth bits by only changing only 1 bit in each steps. Start with the 4th bit as this is the LSB.

The bits of 4-bit gray code are considered as G4G3G2G1. Now from conversion table,

Gray Code

From above SOPs (Sum Of Products), let us draw Karnaugh maps (K-maps) for G4, G3, G2, and G1.

Gray Code K Map
Binary Code K Map
Gray Map
Grey Code To Binary

Application of Gray Code

The gray code is used in a few specific applications. The main applications include being used in analog to digital converters, as well as being used for error correction in digital communication. Gray code is used to minimize errors in converting analog signals to digital signals.

Frank Gray – the person Gray code was named after – patented a Pulse Code Modulation (PCM) tube. This PCM tube was made by Raymond W. Sears of Bell Labs (working with Frank Gray and William M. Goodall) who credited Gray for the idea of the reflected binary code (i.e. gray code). You can read this in television by pulse code modulation.

Some other applications of gray code:

  • Boolean circuit minimization
  • Communication between clock domains
  • Error correction
  • Genetic algorithms
  • Mathematical puzzles
  • Position encoders

Advantages of Gray Code

  • Better for error minimization in converting analog signals to digital signals
  • Reduces the occurrence of “Hamming Walls” (an undesirable state) when used in genetic algorithms
  • Can be used to in to minimize a logic circuit
  • Useful in clock domain crossing

Disadvantages of Gray Code

  • Not suitable for arithmetic operations
  • Limited practical use outside of a few specific applications

About Electrical4U

Electrical4U is dedicated to the teaching and sharing of all things related to electrical and electronics engineering.

Leave a Comment