### Introduction

I have known binary code for sometime now, the basics at least, how they are formed, what values they represent. But recently, I have came across a different kind of binary code.

Here, I am going give a brief explaination how the Gray code works, and what’s so special about it.

### Basic introduction

So how you tell the value of a Gray code? Actually, there is no simple way, at least, its not as simple as a binary system, where you can count like “this is 2 to the power of 1, this is 2 to the power of 2”.

Instead, for Gray code, to get the values, you will need to have a table of values to refer to. But getting the table is really not that hard.

### Understanding the values

Let me give an example of Gray code works. To do that first let me show you a table of what the Gray code values are.

**Table for Gray code**

Decimal |
Gray Code |

0 |
000 |

1 |
001 |

2 |
011 |

3 |
010 |

4 |
110 |

5 |
111 |

6 |
101 |

7 |
100 |

Seen that already? For each consecutive values, like from 0 to 1 or 1 to 2, there is only one bit flip. To make it easier to see, I have highlighted the changed bits to make it more obvious.

But what about the binary code? I have placed the Gray code and typical binary code side by side so you can see the difference in the pattern. For binary code, to jump one value, it might not just have one flipped bit. (Take 1 to 2 for example, you have to “off” the first bit and “on” the second bit)

**Bold refers to changed bits**

Decimal |
Gray Code |
Binary Code |

0 |
000 |
000 |

1 |
00**1** |
001 |

2 |
0**1**1 |
010 |

3 |
01**0** |
011 |

4 |
**1**10 |
100 |

5 |
11**1** |
101 |

6 |
1**0**1 |
110 |

7 |
10**0** |
111 |

### So how do I generate it?

So how do I generate Gray code for values beyond the table? Here’s an example. Let’s say the table is only has 4 values (0 to 3). So how do I go about getting the Gray code for the following values?

To understand how the system works, first follow this flow of logic – assuming that all the Gray code for the previous values are valid (meaning that it take only one bit flip to increment or decrement the value), if we reverse the whole block of code, then the rule will still apply. Let’s pause here and see what we get.

So, here is what we’ll get after the first step.

values |
step 1 |

0 |
00 |

1 |
01 |

2 |
11 |

3 |
10 |

4 |
10 |

5 |
11 |

6 |
01 |

7 |
00 |

So as you can see, the pattern here is as so, the Gray code for the numbers are reversed and placed for the the next set of values. Meaning the pattern for 0 and 7 will match, so does 1 and 6, so on, until we get to 3 and 4.

So how do we tell them apart, the 2 corresponding values? It’s all in the second step. For the actual set of values, we prepend a 0 at the front, and for the second set where we reverse the values, we prepend a 1 at the front.

There we have it, the Gray code for the first 8 values (0 to 7).

values |
step 1 |
step 2 |

0 |
00 |
000 |

1 |
01 |
001 |

2 |
11 |
011 |

3 |
10 |
010 |

4 |
10 |
110 |

5 |
11 |
111 |

6 |
01 |
101 |

7 |
00 |
100 |

### Why is there a need for Gray code

In the past, when computers are all mechanical switches, it made sense to have as little bit flip between increment and decrement, since more bit flip equates to more chances of error.

Also, because of the way some of the mechanical hardware are designed, its easier to use a value table that only have one bit flip for increment and decrement.

If you want to learn more about the system that uses Gray code, you can go here.

### How to convert between Gray code and binary

Well, is there a simple way of converting Gray code to binary? Of course, after all, there is a pattern to these things.

This is a piece of python code written by joshua, it uses the “bitwise exclusive or” operator to do the conversion.

The xor operator is a different topic altogether and I am not going to cover it here. But those interested can go read more about it here and uncover the pattern for yourselves.

def gray2binary(gray):
binary = gray[0]
i = 0
while(len(gray)>i+1):
# the xor operator
binary += `int(binary[i] ^ int(gray[i+1]))`

### Learn more about other kinds of Gray code

What about other arrangment of code that give rise to patterns where increments are one bit flip apart?

Well, this particular type of Gray code that we have discussed here is known as the “binary reversed Gray code”. (after reading so far, I hope you know the reason why)

There are other types of binary Gray code too, there is the balanced Gray code to make the transition between values more uniform and the pattern more balanced. They are other types of binary Gray code too, you can have a look at them out here.

There are Gray code in other number systems too. I mean, you can extend the reverse system idea into other number systems (base-3, base-4, etc). You can see how to adapt the idea here.