This is online lecture series of Nirwaan Foundation. My name is Ahsan Ahmad Ursani. Title of this lecture is ‘Binary-Gray Code Conversion’. In this lecture, we’ll know what is Gray code? how to write Gray code? What are the applications of Gray code? how to convert a binary number to Gray code? and how to convert Gray code back to binary number? This 4-bit binary number, 0010, is equivalent to decimal number 2. Its equivalent Gray code is 0011. It means Gray code too, like binary numbers, comprises a string of 0s and 1s. However, binary numbers are based on weights of 8,4,2,1 etc., i.e. powers of two, whereas Gray code is not based on these weights. These are binary numbers equivalents of decimal 0 through 7, and their corresponding Gray codes. Notice a property of Gray codes, Gray codes change by 1 bit only while going from 0 to 1, 1 to 2, 2 to 3, and so on. For example, while going from 0 to 1, it is only bit g0 that changes its state. Whereas, while going from 1 to 2, it is only bit g1 that changes its state. And in the end, while going back from 7 to 0, it is only bit g2 that changes its state. This means, while going one-step forwards or backwards only one bit change occurs in Gray code. However, this is not the case in ordinary binary numbers. For example, while going from 1 to 2, two bits change their state at the same time. That is, b0 changes from 1 to 0, and b1 changes from 0 to 1. Note that while going from 3 to 4, all the three bits change their state simultaneously. And while going back from 7 to 0, all the three bits change their state simultaneously. Now we will learn to write Gray numbers from 0 to 15. First, we write Gray codes of 0 through 3. First two numbers, i.e. 0 and 1 remain the same in Gray code and ordinary binary notation; i.e., 00 and 01. But, binary equivalent of 3, i.e. (11) becomes Gray code equivalent of 2, and binary equivalent of 2, i.e. (10) becomes Gray code equivalent of 3. That is, one interchanges the positions of 10 and 11 to write Gray code. Now we will write Gray codes of 4 through 7. For this, we introduce bits b2 and g2. Remember that the Most Significant Bit always remains the same. You must have noticed that in case of first four numbers, g1=b1. Similarly, in case of first eight numbers, g2=b2, whereas, g1 and g0 will be mirror image of the above sequence. Place mirror between 3 and 4, and reproduce mirror images of g1 and g0. g1 g0 corresponding to decimal 4 are the same as those of 3. Similarly, g1 g0 corresponding to decimal 5 are the same as those of 2, g1 g0 corresponding to decimal 6 are the same as those of 1, and g1 g0 corresponding to decimal 7 are the same as those of 0. We have written Gray codes from 0 to 7, which can be extended to 15 by introducing fourth bit, i.e. g3. This time too, g3=b3. And we put a mirror between 7 and 8. First three bits g2 g1 g0 for 8 remain the same as those for 7, and so on… Now we will see the applications of Gray code. We use K-maps to design digital logic systems. K-maps are based on Gray codes. Optical Shaft Encoders are commonly used with electric motors. The codes used by these shaft encoders are the Gray codes. It is a disk with Gray code on it. Apart from this, Synchronous digital counters also use Gray codes. Now we will design a digital logic system that converts binary numbers to Gray codes. We know that the Most Significant Bit always remains unchanged. That is, g2=b2, whereas g1 equals b2 Exclusive OR b1. Similarly, g0 equals b1 Exclusive OR b0. That is, we determine g0 by performing Exclusive OR operation between b1 and b0. For example, we convert this 4-bit binary number, 1101, to Gray code. The Most Significant Bit remains the same. That means, this 1 remains a 1. Now we perform Exclusive OR between b3 and b2. 1 XOR 1=0. Now we perform Exclusive OR between b2 and b1. 1 XOR 0=1. And finally, we perform Exclusive OR between b1 and b0. 0 XOR 1=1. It means, Gray code equivalent of binary number 1101 is 1011. In conclusion, for the 4-bit conversion, g3=b3, g2=b3 XOR b2. g1=b2 XOR b1. g0=b1 XOR b0. You find a direct connection between g3 and b3, whereas g2 comes from output of an XOR gate. Similarly, g1 and g0 also come from outputs of XOR gates. On the left hand side of this diagram, one finds b bits on the inputs, and on the right hand side of this diagram, one finds g bits on the outputs. You can extend this very easily to 5 bits, by adding one more XOR gate to the diagram. Now we will make a digital logic system for converting Gray codes to binary numbers. We know that the Most Significant Bit always remains the same. Therefore, b2=g2, whereas we can determine b1 by performing Exclusive OR operation over g1 and b2. That is, b1=g1 XOR b2. Similarly, we can determine the Least Significant Bit b0 by performing Exclusive OR operation over g0 and b1. That is, b0=g0 XOR b1. For the 4-bit conversions, we conclude, b3=g3, b2=b3 XOR g2, b1=b2 XOR g1, and b0=b1 XOR g0. Consequently, we have this logic diagram. Gray bits g0 through g3 remain on the left hand side, i.e. the input of the system, binary bits b0 through b3 remain of the right hand side. i.e. output of the system, whereas the system comprises 3 XOR gates. As an example, we will convert this 4-bit Gray number 1011, back to binary. The Most Significant Bit always remains the same. It means this 1 remains 1. Now we perform XOR operation between b3 and g2 1 XOR 0=1. Now we perform XOR operation between b2 and g1 Now we perform XOR operation between b1 and g0 0 XOR 1=1. Therefore, binary number equivalent of Gray code 1011 is 1101. This lecture concludes here. If you liked this lecture, give a thumbs up. Keep watching videos of Sindhi Science Channel, and subscribe to Sindhi Science Channel. Do not forget expressing your views in the comments.  