# Grey Code in Digital Electronics

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 b0, b1, b2, b3 and corresponding bits in grey code representation are denoted by g0, g1, g2, g3.  Grey code equivalent of the given binary number is computed as follows:

g3 = b3
g2 = b3 ⊕ b2
g1 = b2 ⊕ b1
g0 = b1 ⊕ b0

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:

g3 = b3 = 0
g2 = b3 ⊕ b2 = 0 ⊕ 1 = 1
g1 = b2 ⊕ b1 = 1 ⊕ 0 = 1
g0 = b1 ⊕ b0 = 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

b3 = g3
b2 = b3 ⊕ g2
b1 = b2 ⊕ g1
b0 = b1 ⊕ g0

Thus, binary equivalent of 1101 (grey code) can be computed as follows:

b3 = g3 = 1
b2 = b3 ⊕ g2 = 1 ⊕ 1 = 0
b1 = b2 ⊕ g1 = 0 ⊕ 0 = 0
b0 = b1 ⊕ g0 = 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.

1. 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.

2. good

3. 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.