Similar to Excess-3 Code, Grey Codes are unweighted codes i.e. these codes are not positionally weighted. In a weighted code, a fixed value or weight is assigned to each position with in a binary number. This is not the case with unweighted codes.
Now, let’s say bits of a 4 bit binary number from right are denoted by b_{0}, b_{1}, b_{2}, b_{3} and corresponding bits in grey code representation are denoted by g_{0}, g_{1}, g_{2}, g_{3}. Grey code equivalent of the given binary number is computed as follows:
g_{3} = b_{3}
g_{2} = b_{3} ⊕ b_{2}
g_{1} = b_{2} ⊕ b_{1}
g_{0} = b_{1} ⊕ b_{0}
Here, ⊕ is a symbol for XOR. For a quick reference below is the truth table of XOR:
A |
B |
XOR |
0 |
0 |
0 |
0 |
1 |
1 |
1 |
0 |
1 |
1 |
1 |
0 |
Let’s understand this by taking an example. To find the grey code equivalent of 5, first convert it into binary format i.e. 0101 and then compute the equivalent as below:
g_{3} = b_{3} = 0
g_{2} = b_{3} ⊕ b_{2} = 0 ⊕ 1 = 1
g_{1} = b_{2} ⊕ b_{1} = 1 ⊕ 0 = 1
g_{0} = b_{1} ⊕ b_{0} = 0 ⊕ 1 = 1
Thus grey code equivalent of 5 is 0111. Below is the table displaying grey code equivalent of decimal numbers (0-7):
Decimal Number |
Binary Number |
Grey Code Equivalent |
0 |
0000 |
0000 |
1 |
0001 |
0001 |
2 |
0010 |
0011 |
3 |
0011 |
0010 |
4 |
0100 |
0110 |
5 |
0101 |
0111 |
6 |
0110 |
0101 |
7 |
0111 |
0100 |
Converting from Grey to Binary
b_{3} = g_{3}
b_{2} = b_{3} ⊕ g_{2}
b_{1} = b_{2} ⊕ g_{1}
b_{0} = b_{1} ⊕ g_{0}
Thus, binary equivalent of 1101 (grey code) can be computed as follows:
b_{3} = g_{3} = 1
b_{2} = b_{3} ⊕ g_{2} = 1 ⊕ 1 = 0
b_{1} = b_{2} ⊕ g_{1} = 0 ⊕ 0 = 0
b_{0} = b_{1} ⊕ g_{0} = 0 ⊕ 1 = 1
Thus, binary equivalent of 1101 is 1001.
Grey code is also known as reflected code as three of the bits are reflected as it is and only one bit will change on going from one step to the next. Thus, advantage of using grey codes is that between any two consecutive numbers, only one bit gets changed at a time and that reduces the possibility of errors. Grey code, therefore, belongs to a class of codes called minimum change codes.
Really thanks for it, i was reading on this site (http://morkalork.com/mork/article/74/How_to_understand_and_use_Gray_code.htm) and they told me only that it was the result of a XOR bitwise operation, so i thought that the generation or the grey codes were result of 2 binary numbers, like (000 XOR 001 = 001), (001 XOR 010 = 011), but when i went a little further, the numbers simply didn’t match. Your article helped me to solve it. The nature of the operation is between the bits of the first grey code and not between two binaries – i’m also curious, i guess bitwise means that.
Thanks Gustavo for such a nice feedback. We are glad that the explanation helped you.
Keep visiting Vertical Horizons.
good
nice explanation
i had been given an assignment to get the relationship between the gray codes,binary codes and the BCD codes and your work has helped me greatly so keep it rolling.
Thank you
thank you,that was really helpful