DEV Community

Cover image for Exploring Gray Code – The Oddball Sibling of Binary
Max Feige
Max Feige

Posted on • Originally published at maxfeige.dev

Exploring Gray Code – The Oddball Sibling of Binary

So hopefully we're all familiar with binary. Just in case, here's a review:

Binary Counting

That's the standard way of counting in binary. And for computers, this is a perfectly logical and optimal system. Its the way most numbers on our computer work.*

But what if we had to manually track the bits ourselves using, say, a series of heavy switches to store a counter, like counting people who enter a store? Let's take a look at our hypothetical counter:

Binary Switches 0

After some period of time, we've had several people come in. So by now, we have 7 people: Here it is stored in binary with our counter.

Binary Counting 7

Suddenly we hear the bell ding; someone else has entered the store. So we should increase the counter to eight:

Binary Counting 8

But man - that was hard! The effort required to flip multiple switches can quickly become overwhelming as the count increases. You might wonder if there's a more efficient method - and there is. It's called Gray Code.

Gray Code is an alternative binary numeral system where two successive values differ in only one bit, which means flipping just one switch to go from one value to the next. Here's what counting from 0 to 7 looks like in Gray Code:

Gray Code 0 to 7

Hopefully you were able to see the pattern - each time we flip the switch furthest to the right that will give us a combination we haven't used. You can read more about Gray Code here.

You might think that converting to and from Gray Code would be complicated - but thankfully some really smart people have created some clever algorithms that make it relatively straight forward. Here they are:

Binary to gray pseudocode

Gray to binary pseudocode

And that's how all we need!

Now, let's have some fun with Gray Code! As I demonstrated in my last article, you can create interesting images using the bit representation of numbers. Let's see how different bitwise operators - AND, OR, XOR - behave when applied to Gray Code:

Note: These are stretched a bit in the x direction, and I tend to like them that way. If you want to see the square images, resizing the images should give you something indistinguishable.

First we can compare normal binary to gray code.

Binary gradient based on x coordinate

Gray code gradient based on x coordinate

Now lets look at our bitwise operators. We'll consider two cases: First we convert our numbers to gray code, perform the bitwise operator, then convert them back to decimal.

Normal OR

Normal AND

Normal XOR

You might notice something peculiar about the image showcasing the Normal XOR operation. The apparent 'blockiness' isn't a result of image artifact compression or a mistake - it's actually an intriguing characteristic that arises from the Gray Code representation!

What's even more fascinating is that this Normal Gray Code XOR operation yields a result entirely equivalent to the binary XOR operation. This might seem counter-intuitive - I encourage you to take a look at the previous code and see if you can solve this puzzle. Unpacking these mysteries is part of what makings coding so exciting!

Now let's consider the concept of "casting". By casting, I mean performing our operation in Gray Code but not converting the numbers back to binary. While not typically used, this process can generate some fascinating patterns:

Inspired by this famous algorithm

Casted OR

Casted AND

Casted XOR

I find these patterns absolutely captivating. The process of casting in Gray Code produces an effect reminiscent of layering transparent images, creating a captivating visual representation of this weird binary sibling.

Top comments (0)