I used to get annoyed when I would have to write code that was sort of ugly, but I had to use anyway because it was the only way I could figure out how to get the job done. Maybe there is a better way, or maybe not – or maybe I’m just not smart enough. Who knows?
Here are some examples of grey code – code that does what you need it to do, but might be ugly. My hope is that you’ll see some tips and tricks here that you can use in your own code.
Grey code is a way to reflect binary numbers in a non-weighted manner. The idea behind this is to create a system that uses the same amount of digits no matter the scale of the number you are converting.
0 = 000
1 = 001
2 = 011
3 = 010
4 = 110
5 = 111
6 = 101
7 = 100
This is useful for things like rotary encoders and joystick control systems. These two components do not use an analog signal, but rather a digital one that reads as on/off. Using binary it would be easy for these circuits to get confused when going from 7 to 8, as it would skip from 111 to 1000. If we were using true binary, which counts in powers of 2, then 8 would be represented as 1000, 9 would be 1001, and so on. But if we use grey code instead, it shows the steps more clearly. Using grey code, going from 7 to 8 becomes 7→111→100→8.
Grey code is a binary code where two successive values differ in only one bit, as well as when wrapping around. The reflected binary code, also called Gray code after Frank Gray, is a binary numeral system where two successive values differ in only one digit. It is also known as the reflected binary code (RBC), or Gray code after Frank Gray. Other codes with this property are the single-distance codes.
The code can be generated in a simple way: start with 0 and 1, then repeatedly prefix all entries of the current list with 0 and 1, but in reverse order. For example, to generate a 3-bit Gray code:
Start: 0 1
Prefix reversed: 00 01 11 10
There are other ways to construct Gray codes with the same number of elements that do not follow the pattern above. One such method is to start with any Gray code for n − 1 bits and append a 1 to each element (changing its parity). Then reverse the order of these n elements to obtain another valid n-bit Gray code. Another common method is to construct an n-bit cyclic Gray code by starting with one bit per cycle and doubling the number of bits per cycle until reaching n bits total.
Mostly when we think about binary numbers, we imagine a series of 0’s & 1’s. For instance, 1011 is a binary number. But if you ever have worked with computers, you may have heard of Grey Code. In this article, we will talk about what is Grey Code and why it is useful.
Binary numbers are used to store numbers in computers but they can also be used as a way to transmit data between devices.
For example, consider the case of transmitting the bits 1011 from one device to another. We send it bit by bit starting from the MSB (most significant bit), that is 1 first and then 0 then 1 and finally 1 again.
Now let us assume that during transmission, there was a sudden change in voltage level leading to some noise which caused the receiver system to wrongly interpret the third bit as 0 instead of correctly reading it as 1. This can lead to catastrophic results.
This problem occurs because the input value changed by 2 units between two consecutive bits, which is too much for noise-prone systems or even human perception systems (as humans are not very good at comparing two binary numbers).
Grey code is a binary numeral system where two successive values differ in only one bit. In this post, I am going to discuss how to derive the gray code for a given number of bits. This is very useful in many applications such as encoders and decoders.
I was inspired by this video from 3blue1brown to write this blog. He explains it in a much better way than I could. So make sure to watch his video if you want a better explanation of what is a Grey code and why it’s useful: The Hidden Gem of Mathematics — The Gray Code
Binary numbers have many applications in digital electronics and computer science. However, sometimes we need an in-between representation that allows us to almost convert from one binary number to another with only one bit change at each step.
For example, let’s say you have 4 switches connected to a LED through an OR gate as shown below:
Coding is a craft.
This means that it takes time and effort to learn it but once you have learned it you can make beautiful things.
Here are some tips and tricks I’ve collected along the way that might help you on your journey to becoming a better coder.
1. The first step to coding well is abstracting stuff. The best coders are the ones who can take a complicated problem and reduce it down into simple pieces. This is not an easy thing to do, and most people don’t get very good at it until they have 10+ years of experience. But being able to break problems apart and think about them in different ways will make you a much better coder than if you can just type fast or remember lots of trivia.
2. Once you’ve abstracted something, try to generalize it as much as possible. If you find yourself repeating code or using similar algorithms over again, there’s probably a better way to organize your program so that those patterns become more explicit instead of being hidden inside each subroutine.
3. When in doubt, go back to basics! It’s easy for experienced programmers to forget how hard some concepts were when they first learned them (e.g., pointers) but remember that everyone
The binary reflected Gray code list for n bits can be generated recursively from the list for n − 1 bits by reflecting the list (i.e. listing the entries in reverse order), concatenating the original list with the reversed list, prefixing the entries in the original list with a binary 0, and then prefixing the entries in the reflected list with a binary 1.
For example, generating the n = 3 list from the n = 2 list:
2-bit list: 00 01 11 10
Reversed: 10 11 01 00
Prefix old entries with 0: 000 001 011 010
Prefix new entries with 1: 110 111 101 100
Concatenated: 000 001 011 010 110 111 101 100
The Gray code is named after Frank Gray, who patented it in 1953. A Gray code can be used to facilitate error correction in digital communications such as digital terrestrial television and some cable TV systems.