# Gray code

Gray code . It consists of an ordering of [itex] 2 ^ n [/itex] binary numbers in such a way that each number only has one binary digitdifferent from its predecessor. This coding technique originated when digital logic circuits were made with vacuum valves and electromechanical devices. The counters required very high input powers and generated noise peaks when several bits changed simultaneously. The use of Gray code guaranteed that in any transition it would vary only one bit. At present, the Gray code is still used for the design of any combinational electronic circuit through the use of a Karnaugh Map, since the design principle of looking for simpler and faster transitions between states is still valid, despite the fact that the noise and power problems have been reduced. There are several algorithms to generate a Gray code sequence (and several possible resulting codes, depending on the order you want to follow), but the most used is to change the least significant bit that generates a new code. This is a four-bit gray code generated with this algorithm:

Decimal digit Gray code Decimal digit Gray code

0 0000 8 1100

1 0001 9 1101

2 0011 10 1111

3 0010 11 1110

4 0110 12 1010

5 0111 13 1011

6 0101 14 1001

7 0 100 15 1000

The first documented use of such a code was in a telegraph demonstration by the French engineer Émile Baudot, in 1878. But they were not patented until 1953 by Frank Gray (who gave the coding system its name), a researcher at Bell Laboratories. .

Summary

[ hide ]

• 1 Binary to Gray
• 1 Other Examples
• 2 Gray to Binari
• 3 Source

Binary to Gray

To pass a binary number to the Gray binary code, there is an easy rule to implement in a programming language:

• A binary number always starts at 1 – Leading zeros do not count -; Well, in Gray too. Ex: 1000011110000 in binary it is written 1xxxxXXXXxxxx.
• Now we look at the second digit. If it is equal to the previous digit, a 0 is put (it does not change); If it is different – as is the case, since the previous digit was a 1 and the one that we observed a 0 – it will be put a 1 (it changes). Ex: The number in the previous example will be: 11xxxXXXXxxxx.
• In successive cases, the previous step is repeated, observing in the ‘natural’ binary number the digit before the one being evaluated. Ex: The number of the previous example, passed to Gray code will be: 1100010001000.

Other examples:

1010 – 1111

111000 – 100 100

011001 – 010101

110101010001 – 101111111001

Another simple technique to go from binary to Gray without using a programming language is this:

• apply an XOR (exclusive disjunction, you can also add each bit individually discarding any carry) of the number to itself but with a right carry
• remove the far right bit

Ex: Passing ten (1010) from binary to gray will be 1111

1010

1010

—–

1111

Other examples

111000

111000

——

100 100

110101010001

110101010001

————

101111111001

Gray to Binari

To do the opposite change is simply to invert what the previous one does, so that if a zero is found (as long as it is not at the beginning), the previous figure must be entered; On the other hand, if it puts a 1 it is because the figure has changed so if there was a 0 now it is put a 1 and vice versa.