The reflected binary code (RBC), also known as reflected binary (RB) or Gray code after Frank Gray, is an ordering of the binary numeral system such that two successive values differ in only one bit (binary digit).
For example, the representation of the decimal value "1" in binary would normally be "" and "2" would be "". In Gray code, these values are represented as "" and "". That way, incrementing a value from 1 to 2 requires only one bit to change, instead of two.
Gray codes are widely used to prevent spurious output from electromechanical switches and to facilitate error correction in digital communications such as digital terrestrial television and some cable TV systems. The use of Gray code in these devices helps simplify logic operations and reduce errors in practice.[1]
Many devices indicate position by closing and opening switches. If that device uses natural binary codes, positions 3 and 4 are next to each other but all three bits of the binary representation differ:
Decimal | Binary | |
---|---|---|
... | ... | |
3 | ||
4 | ||
... | ... |
The problem with natural binary codes is that physical switches are not ideal: it is very unlikely that physical switches will change states exactly in synchrony. In the transition between the two states shown above, all three switches change state. In the brief period while all are changing, the switches will read some spurious position. Even without keybounce, the transition might look like — — — . When the switches appear to be in position, the observer cannot tell if that is the "real" position 1, or a transitional state between two other positions. If the output feeds into a sequential system, possibly via combinational logic, then the sequential system may store a false value.
This problem can be solved by changing only one switch at a time, so there is never any ambiguity of position, resulting in codes assigning to each of a contiguous set of integers, or to each member of a circular list, a word of symbols such that no two code words are identical and each two adjacent code words differ by exactly one symbol. These codes are also known as unit-distance, single-distance, single-step, monostrophic or syncopic codes, in reference to the Hamming distance of 1 between adjacent codes.
In principle, there can be more than one such code for a given word length, but the term Gray code was first applied to a particular binary code for non-negative integers, the binary-reflected Gray code, or BRGC. Bell Labs researcher George R. Stibitz described such a code in a 1941 patent application, granted in 1943. Frank Gray introduced the term reflected binary code in his 1947 patent application, remarking that the code had "as yet no recognized name". He derived the name from the fact that it "may be built up from the conventional binary code by a sort of reflection process".
In the standard encoding of the Gray Code the least significant bit follows a repetitive pattern of 2 on, 2 off the next digit a pattern of 4 on, 4 off; the i-th least significant bit a pattern of 2i on 2i off. The most significant digit is an exception to this: for an n-bit Gray code, the most significant digit follows the pattern 2n-1 on, 2n-1 off, which is the same (cyclic) sequence of values as for the second-most significant digit, but shifted forwards 2n-2 places. The four-bit version of this is shown below:
Decimal | Binary | Gray | |
---|---|---|---|
0 | |||
1 | |||
2 | |||
3 | |||
4 | |||
5 | |||
6 | |||
7 | |||
8 | |||
9 | |||
10 | |||
11 | |||
12 | |||
13 | |||
14 | |||
15 |
For decimal 15 the code rolls over to decimal 0 with only one switch change. This is called the cyclic or adjacency property of the code.
In modern digital communications, Gray codes play an important role in error correction. For example, in a digital modulation scheme such as QAM where data is typically transmitted in symbols of 4 bits or more, the signal's constellation diagram is arranged so that the bit patterns conveyed by adjacent constellation points differ by only one bit. By combining this with forward error correction capable of correcting single-bit errors, it is possible for a receiver to correct any transmission errors that cause a constellation point to deviate into the area of an adjacent point. This makes the transmission system less susceptible to noise.
Despite the fact that Stibitz described this code before Gray, the reflected binary code was later named after Gray by others who used it. Two different 1953 patent applications use "Gray code" as an alternative name for the "reflected binary code"; one of those also lists "minimum error code" and "cyclic permutation code" among the names. A 1954 patent application refers to "the Bell Telephone Gray code". Other names include "cyclic binary code", "cyclic progression code", "cyclic permuting binary" or "cyclic permuted binary" (CPB).
The Gray code is sometimes misattributed to 19th century electrical device inventor Elisha Gray.
Reflected binary codes were applied to mathematical puzzles before they became known to engineers.
The binary-reflected Gray code represents the underlying scheme of the classical Chinese rings puzzle, a sequential mechanical puzzle mechanism described by the French Louis Gros in 1872.
It can serve as a solution guide for the Towers of Hanoi problem, based on a game by the French Édouard Lucas in 1883. Similarly, the so-called Towers of Bucharest and Towers of Klagenfurt game configurations yield ternary and pentary Gray codes.
Martin Gardner wrote a popular account of the Gray code in his August 1972 Mathematical Games column in Scientific American.
The code also forms a Hamiltonian cycle on a hypercube, where each bit is seen as one dimension.
When the French engineer Émile Baudot changed from using a 6-unit (6-bit) code to 5-unit code for his printing telegraph system, in 1875 or 1876, he ordered the alphabetic characters on his print wheel using a reflected binary code, and assigned the codes using only three of the bits to vowels. With vowels and consonants sorted in their alphabetical order, and other symbols appropriately placed, the 5-bit character code has been recognized as a reflected binary code. This code became known as Baudot code and, with minor changes, was eventually adopted as International Telegraph Alphabet No. 1 (ITA1, CCITT-1) in 1932. About the same time, the German-Austrian demonstrated another printing telegraph in Vienna using a 5-bit reflected binary code for the same purpose, in 1874.
Frank Gray, who became famous for inventing the signaling method that came to be used for compatible color television, invented a method to convert analog signals to reflected binary code groups using vacuum tube-based apparatus. Filed in 1947, the method and apparatus were granted a patent in 1953, and the name of Gray stuck to the codes. The "PCM tube" apparatus that Gray patented was made by Raymond W. Sears of Bell Labs, working with Gray and William M. Goodall, who credited Gray for the idea of the reflected binary code.
Gray was most interested in using the codes to minimize errors in converting analog signals to digital; his codes are still used today for this purpose.
Gray codes are used in linear and rotary position encoders (absolute encoders and quadrature encoders) in preference to weighted binary encoding. This avoids the possibility that, when multiple bits change in the binary representation of a position, a misread will result from some of the bits changing before others.
For example, some rotary encoders provide a disk which has an electrically conductive Gray code pattern on concentric rings (tracks). Each track has a stationary metal spring contact that provides electrical contact to the conductive code pattern. Together, these contacts produce output signals in the form of a Gray code. Other encoders employ non-contact mechanisms based on optical or magnetic sensors to produce the Gray code output signals.
Regardless of the mechanism or precision of a moving encoder, position measurement error can occur at specific positions (at code boundaries) because the code may be changing at the exact moment it is read (sampled). A binary output code could cause significant position measurement errors because it is impossible to make all bits change at exactly the same time. If, at the moment the position is sampled, some bits have changed and others have not, the sampled position will be incorrect. In the case of absolute encoders, the indicated position may be far away from the actual position and, in the case of incremental encoders, this can corrupt position tracking.
In contrast, the Gray code used by position encoders ensures that the codes for any two consecutive positions will differ by only one bit and, consequently, only one bit can change at a time. In this case, the maximum position error will be small, indicating a position adjacent to the actual position.
Due to the Hamming distance properties of Gray codes, they are sometimes used in genetic algorithms. They are very useful in this field, since mutations in the code allow for mostly incremental changes, but occasionally a single bit-change can cause a big leap and lead to new properties.
Gray codes are also used in labelling the axes of Karnaugh maps since 1953 as well as in Händler circle graphs since 1958, both graphical methods for logic circuit minimization.
In modern digital communications, 1D- and 2D-Gray codes play an important role in error prevention before applying an error correction. For example, in a digital modulation scheme such as QAM where data is typically transmitted in symbols of 4 bits or more, the signal's constellation diagram is arranged so that the bit patterns conveyed by adjacent constellation points differ by only one bit. By combining this with forward error correction capable of correcting single-bit errors, it is possible for a receiver to correct any transmission errors that cause a constellation point to deviate into the area of an adjacent point. This makes the transmission system less susceptible to noise.
See main article: article and Clock domain crossing.
Digital logic designers use Gray codes extensively for passing multi-bit count information between synchronous logic that operates at different clock frequencies. The logic is considered operating in different "clock domains". It is fundamental to the design of large chips that operate with many different clocking frequencies.
If a system has to cycle sequentially through all possible combinations of on-off states of some set of controls, and the changes of the controls require non-trivial expense (e.g. time, wear, human work), a Gray code minimizes the number of setting changes to just one change for each combination of states. An example would be testing a piping system for all combinations of settings of its manually operated valves.
A balanced Gray code can be constructed, that flips every bit equally often. Since bit-flips are evenly distributed, this is optimal in the following way: balanced Gray codes minimize the maximal count of bit-flips for each digit.
George R. Stibitz utilized a reflected binary code in a binary pulse counting device in 1941 already.
A typical use of Gray code counters is building a FIFO (first-in, first-out) data buffer that has read and write ports that exist in different clock domains. The input and output counters inside such a dual-port FIFO are often stored using Gray code to prevent invalid transient states from being captured when the count crosses clock domains.The updated read and write pointers need to be passed between clock domains when they change, to be able to track FIFO empty and full status in each domain. Each bit of the pointers is sampled non-deterministically for this clock domain transfer. So for each bit, either the old value or the new value is propagated. Therefore, if more than one bit in the multi-bit pointer is changing at the sampling point, a "wrong" binary value (neither new nor old) can be propagated. By guaranteeing only one bit can be changing, Gray codes guarantee that the only possible sampled values are the new or old multi-bit value. Typically Gray codes of power-of-two length are used.
Sometimes digital buses in electronic systems are used to convey quantities that can only increase or decrease by one at a time, for example the output of an event counter which is being passed between clock domains or to a digital-to-analog converter. The advantage of Gray codes in these applications is that differences in the propagation delays of the many wires that represent the bits of the code cannot cause the received value to go through states that are out of the Gray code sequence. This is similar to the advantage of Gray codes in the construction of mechanical encoders, however the source of the Gray code is an electronic counter in this case. The counter itself must count in Gray code, or if the counter runs in binary then the output value from the counter must be reclocked after it has been converted to Gray code, because when a value is converted from binary to Gray code, it is possible that differences in the arrival times of the binary data bits into the binary-to-Gray conversion circuit will mean that the code could go briefly through states that are wildly out of sequence. Adding a clocked register after the circuit that converts the count value to Gray code may introduce a clock cycle of latency, so counting directly in Gray code may be advantageous.
To produce the next count value in a Gray-code counter, it is necessary to have some combinational logic that will increment the current count value that is stored. One way to increment a Gray code number is to convert it into ordinary binary code, add one to it with a standard binary adder, and then convert the result back to Gray code. Other methods of counting in Gray code are discussed in a report by Robert W. Doran, including taking the output from the first latches of the master-slave flip flops in a binary ripple counter.
As the execution of program code typically causes an instruction memory access pattern of locally consecutive addresses, bus encodings using Gray code addressing instead of binary addressing can reduce the number of state changes of the address bits significantly, thereby reducing the CPU power consumption in some low-power designs.
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), prefixing the entries in the original list with a binary, prefixing the entries in the reflected list with a binary , and then concatenating the original list with the reversed list. For example, generating the n = 3 list from the n = 2 list:
2-bit list: | ,,, | ||
Reflected: | ,,, | ||
Prefix old entries with : | ,,,, | ||
Prefix new entries with : | ,,, | ||
Concatenated: | ,,,, | ,,, |
The one-bit Gray code is G1 = . This can be thought of as built recursively as above from a zero-bit Gray code G0 = ( Λ ) consisting of a single entry of zero length. This iterative process of generating Gn+1 from Gn makes the following properties of the standard reflecting code clear:
These characteristics suggest a simple and fast method of translating a binary value into the corresponding Gray code. Each bit is inverted if the next higher bit of the input value is set to one. This can be performed in parallel by a bit-shift and exclusive-or operation if they are available: the nth Gray code is obtained by computing
n ⊕ \left\lfloor\tfrac{n}{2}\right\rfloor
i
2i
→ (invert bit 0)
If bit 1 is inverted, blocks of 2 codewords change order:
→ (invert bit 1)
If bit 2 is inverted, blocks of 4 codewords reverse order:
→ (invert bit 2)
Thus, performing an exclusive or on a bit
bi
i
bi+1
i+1
bi+1=0
2i+1
bi+1=1
A similar method can be used to perform the reverse translation, but the computation of each bit depends on the computed value of the next higher bit so it cannot be performed in parallel. Assuming
gi
i
g0
bi
i
b0
b0=g0
bi=gi ⊕ bi-1
To construct the binary-reflected Gray code iteratively, at step 0 start with the
code0=0
i>0
i
codei-1
codei
The following functions in C convert between binary numbers and their associated Gray codes. While it may seem that Gray-to-binary conversion requires each bit to be handled one at a time, faster algorithms exist.
// This function converts an unsigned binary number to reflected binary Gray code.uint BinaryToGray(uint num)
// This function converts a reflected binary Gray code number to a binary number.uint GrayToBinary(uint num)
// A more efficient version for Gray codes 32 bits or fewer through the use of SWAR (SIMD within a register) techniques. // It implements a parallel prefix XOR function. The assignment statements can be in any order.// // This function can be adapted for longer Gray codes by adding steps.
uint GrayToBinary32(uint num)// A Four-bit-at-once variant changes a binary number (abcd)2 to (abcd)2 ^ (00ab)2, then to (abcd)2 ^ (00ab)2 ^ (0abc)2 ^ (000a)2.
On newer processors, the number of ALU instructions in the decoding step can be reduced by taking advantage of the CLMUL instruction set. If MASK is the constant binary string of ones ended with a single zero digit, then carryless multiplication of MASK with the grey encoding of x will always give either x or its bitwise negation.
In practice, "Gray code" almost always refers to a binary-reflected Gray code (BRGC).However, mathematicians have discovered other kinds of Gray codes.Like BRGCs, each consists of a list of words, where each word differs from the next in only one digit (each word has a Hamming distance of 1 from the next word).
It is possible to construct binary Gray codes with n bits with a length of less than, if the length is even. One possibility is to start with a balanced Gray code and remove pairs of values at either the beginning and the end, or in the middle.[2] OEIS sequence A290772 gives the number of possible Gray sequences of length that include zero and use the minimum number of bits.
|
There are many specialized types of Gray codes other than the binary-reflected Gray code. One such type of Gray code is the n-ary Gray code, also known as a non-Boolean Gray code. As the name implies, this type of Gray code uses non-Boolean values in its encodings.
For example, a 3-ary (ternary) Gray code would use the values . The (n, k)-Gray code is the n-ary Gray code with k digits.The sequence of elements in the (3, 2)-Gray code is: . The (n, k)-Gray code may be constructed recursively, as the BRGC, or may be constructed iteratively. An algorithm to iteratively generate the (N, k)-Gray code is presented (in C):
There are other Gray code algorithms for (n,k)-Gray codes. The (n,k)-Gray code produced by the above algorithm is always cyclical; some algorithms, such as that by Guan, lack this property when k is odd. On the other hand, while only one digit at a time changes with this method, it can change by wrapping (looping from n − 1 to 0). In Guan's algorithm, the count alternately rises and falls, so that the numeric difference between two Gray code digits is always one.
Gray codes are not uniquely defined, because a permutation of the columns of such a code is a Gray code too. The above procedure produces a code in which the lower the significance of a digit, the more often it changes, making it similar to normal counting methods.
See also Skew binary number system, a variant ternary number system where at most two digits change on each increment, as each increment can be done with at most one digit carry operation.
Although the binary reflected Gray code is useful in many scenarios, it is not optimal in certain cases because of a lack of "uniformity". In balanced Gray codes, the number of changes in different coordinate positions are as close as possible. To make this more precise, let G be an R-ary complete Gray cycle having transition sequence
(\deltak)
λk=|\{j\in
Z | |
Rn |
:\deltaj=k\}|,fork\inZn
A Gray code is uniform or uniformly balanced if its transition counts are all equal, in which case we have
λk=\tfrac{Rn}{n}
R=2
2\left\lfloor\tfrac{2n}{2n}\right\rfloor
2\left\lceil\tfrac{2n}{2n}\right\rceil
For example, a balanced 4-bit Gray code has 16 transitions, which can be evenly distributed among all four positions (four transitions per position), making it uniformly balanced:
whereas a balanced 5-bit Gray code has a total of 32 transitions, which cannot be evenly distributed among the positions. In this example, four positions have six transitions each, and one has eight:
We will now show a construction and implementation for well-balanced binary Gray codes which allows us to generate an n-digit balanced Gray code for every n. The main principle is to inductively construct an (n + 2)-digit Gray code
G'
G=g0,\ldots,
g | |
2n-1 |
\left\{g0\right\},\left\{g1,\ldots,
g | |
k2 |
\right\},
\left\{g | |
k2+1 |
,\ldots,
g | |
k3 |
\right\},\ldots,
\left\{g | |
kL-2+1 |
,\ldots,g-2\right\},\left\{g-1\right\}
k1=0
kL-1=-2
kL\equiv-1\pmod{2n}
(n+2)
00g0,
00g1,\ldots,
00g | |
k2 |
,
01g | |
k2 |
,\ldots,01g1,11g1,\ldots,
11g | |
k2 |
,
11g | |
k2+1 |
,\ldots,
11g | |
k3 |
,
01g | |
k3 |
,\ldots,
01g | |
k2+1 |
,
00g | |
k2+1 |
,\ldots,
00g | |
k3 |
,\ldots,
00g-2,00g-1,10g-1,10g-2,\ldots,10g0,11g0,11g-1,01g-1,01g0
If we define the transition multiplicities
mi=\left|\left\{j:
\delta | |
kj |
=i,1\leqj\leqL\right\}\right|
λ'i
λ'i=\begin{cases} 4λi-2mi,&if0\leqi<n\\ L,&otherwise \end{cases}
The delicate part of this construction is to find an adequate partitioning of a balanced n-digit Gray code such that the code induced by it remains balanced, but for this only the transition multiplicities matter; joining two consecutive blocks over a digit
i
i
λ'i
mi
i
R\equiv0\pmod4
Rn\equiv0\pmodn
Long run (or maximum gap) Gray codes maximize the distance between consecutive changes of digits in the same position. That is, the minimum run-length of any bit remains unchanged for as long as possible.[4]
Monotonic codes are useful in the theory of interconnection networks, especially for minimizing dilation for linear arrays of processors.If we define the weight of a binary string to be the number of 1s in the string, then although we clearly cannot have a Gray code with strictly increasing weight, we may want to approximate this by having the code run through two adjacent weights before reaching the next one.
We can formalize the concept of monotone Gray codes as follows: consider the partition of the hypercube
Qn=(Vn,En)
Vn(i)=\{v\inVn:vhasweighti\}
0\leqi\leqn
|Vn(i)|=style\binom{n}{i}
Qn(i)
Qn
Vn(i)\cupVn(i+1)
En(i)
Qn(i)
Qn
\delta1\inEn(i)
\delta2\inEn(j)
i\leqj
An elegant construction of monotonic n-digit Gray codes for any n is based on the idea of recursively building subpaths
Pn,j
2style\binom{n}{j}
En(j)
P1,0=(0,1)
Pn,j=\emptyset
j<0
j\geqn
Pn+1,j=
\pin | |
1P | |
n,j-1 |
,0Pn,j
\pin
P\pi
\pi
(1) | |
G | |
n |
(2) | |
G | |
n |
(1) | |
G | |
n |
=Pn,0
R | |
P | |
n,1 |
Pn,2
R | |
P | |
n,3 |
… and
(2) | |
G | |
n |
=
R | |
P | |
n,0 |
Pn,1
R | |
P | |
n,2 |
Pn,3 …
The choice of
\pin
\pin=E-1
2\right) | |
\left(\pi | |
n-1 |
Pn,j
These monotonic Gray codes can be efficiently implemented in such a way that each subsequent element can be generated in O(n) time. The algorithm is most easily described using coroutines.
Monotonic codes have an interesting connection to the Lovász conjecture, which states that every connected vertex-transitive graph contains a Hamiltonian path. The "middle-level" subgraph
Q2n+1(n)
n\leq15
Another type of Gray code, the Beckett–Gray code, is named for Irish playwright Samuel Beckett, who was interested in symmetry. His play "Quad" features four actors and is divided into sixteen time periods. Each period ends with one of the four actors entering or leaving the stage. The play begins and ends with an empty stage, and Beckett wanted each subset of actors to appear on stage exactly once. Clearly the set of actors currently on stage can be represented by a 4-bit binary Gray code. Beckett, however, placed an additional restriction on the script: he wished the actors to enter and exit so that the actor who had been on stage the longest would always be the one to exit. The actors could then be represented by a first in, first out queue, so that (of the actors onstage) the actor being dequeued is always the one who was enqueued first. Beckett was unable to find a Beckett–Gray code for his play, and indeed, an exhaustive listing of all possible sequences reveals that no such code exists for n = 4. It is known today that such codes do exist for n = 2, 5, 6, 7, and 8, and do not exist for n = 3 or 4. An example of an 8-bit Beckett–Gray code can be found in Donald Knuth's Art of Computer Programming. According to Sawada and Wong, the search space for n = 6 can be explored in 15 hours, and more than solutions for the case n = 7 have been found.
Snake-in-the-box codes, or snakes, are the sequences of nodes of induced paths in an n-dimensional hypercube graph, and coil-in-the-box codes, or coils, are the sequences of nodes of induced cycles in a hypercube. Viewed as Gray codes, these sequences have the property of being able to detect any single-bit coding error. Codes of this type were first described by William H. Kautz in the late 1950s; since then, there has been much research on finding the code with the largest possible number of codewords for a given hypercube dimension.
Yet another kind of Gray code is the single-track Gray code (STGC) developed by Norman B. Spedding and refined by Hiltgen, Paterson and Brandestini in Single-track Gray Codes (1996). The STGC is a cyclical list of P unique binary encodings of length n such that two consecutive words differ in exactly one position, and when the list is examined as a P × n matrix, each column is a cyclic shift of the first column.
The name comes from their use with rotary encoders, where a number of tracks are being sensed by contacts, resulting for each in an output of or . To reduce noise due to different contacts not switching at exactly the same moment in time, one preferably sets up the tracks so that the data output by the contacts are in Gray code. To get high angular accuracy, one needs lots of contacts; in order to achieve at least 1° accuracy, one needs at least 360 distinct positions per revolution, which requires a minimum of 9 bits of data, and thus the same number of contacts.
If all contacts are placed at the same angular position, then 9 tracks are needed to get a standard BRGC with at least 1° accuracy. However, if the manufacturer moves a contact to a different angular position (but at the same distance from the center shaft), then the corresponding "ring pattern" needs to be rotated the same angle to give the same output. If the most significant bit (the inner ring in Figure 1) is rotated enough, it exactly matches the next ring out. Since both rings are then identical, the inner ring can be cut out, and the sensor for that ring moved to the remaining, identical ring (but offset at that angle from the other sensor on that ring). Those two sensors on a single ring make a quadrature encoder. That reduces the number of tracks for a "1° resolution" angular encoder to 8 tracks. Reducing the number of tracks still further cannot be done with BRGC.
For many years, Torsten Sillke and other mathematicians believed that it was impossible to encode position on a single track such that consecutive positions differed at only a single sensor, except for the 2-sensor, 1-track quadrature encoder. So for applications where 8 tracks were too bulky, people used single-track incremental encoders (quadrature encoders) or 2-track "quadrature encoder + reference notch" encoders.
Norman B. Spedding, however, registered a patent in 1994 with several examples showing that it was possible. Although it is not possible to distinguish 2n positions with n sensors on a single track, it is possible to distinguish close to that many. Etzion and Paterson conjecture that when n is itself a power of 2, n sensors can distinguish at most 2n − 2n positions and that for prime n the limit is 2n − 2 positions. The authors went on to generate a 504-position single track code of length 9 which they believe is optimal. Since this number is larger than 28 = 256, more than 8 sensors are required by any code, although a BRGC could distinguish 512 positions with 9 sensors.
An STGC for P = 30 and n = 5 is reproduced here:
Code | Angle | Code | Angle | Code | Angle | Code | Angle | Code | ||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
0° | 72° | 144° | 216° | 288° | ||||||||
12° | 84° | 156° | 228° | 300° | ||||||||
24° | 96° | 168° | 240° | 312° | ||||||||
36° | 108° | 180° | 252° | 324° | ||||||||
48° | 120° | 192° | 264° | 336° | ||||||||
60° | 132° | 204° | 276° | 348° |
Each column is a cyclic shift of the first column, and from any row to the next row only one bit changes.The single-track nature (like a code chain) is useful in the fabrication of these wheels (compared to BRGC), as only one track is needed, thus reducing their cost and size.The Gray code nature is useful (compared to chain codes, also called De Bruijn sequences), as only one sensor will change at any one time, so the uncertainty during a transition between two discrete states will only be plus or minus one unit of angular measurement the device is capable of resolving.
Since this 30 degree example was added, there has been a lot of interest in examples with higher angular resolution. In 2008, Gary Williams,[5] based on previous work discovered a 9-bit Single Track Gray Code that gives a 1 degree resolution. This gray code was used to design an actual device which was published on the site Thingiverse. This device[6] was designed by etzenseep (Florian Bauer) in September, 2022.
An STGC for P = 360 and n = 9 is reproduced here:
Code | Angle | Code | Angle | Code | Angle | Code | Angle | Code | Angle | Code | Angle | Code | Angle | Code | Angle | Code | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0° | 40° | 80° | 120° | 160° | 200° | 240° | 280° | 320° | |||||||||||||||||
1° | 41° | 81° | 121° | 161° | 201° | 241° | 281° | 321° | |||||||||||||||||
2° | 42° | 82° | 122° | 162° | 202° | 242° | 282° | 322° | |||||||||||||||||
3° | 43° | 83° | 123° | 163° | 203° | 243° | 283° | 323° | |||||||||||||||||
4° | 44° | 84° | 124° | 164° | 204° | 244° | 284° | 324° | |||||||||||||||||
5° | 45° | 85° | 125° | 165° | 205° | 245° | 285° | 325° | |||||||||||||||||
6° | 46° | 86° | 126° | 166° | 206° | 246° | 286° | 326° | |||||||||||||||||
7° | 47° | 87° | 127° | 167° | 207° | 247° | 287° | 327° | |||||||||||||||||
8° | 48° | 88° | 128° | 168° | 208° | 248° | 288° | 328° | |||||||||||||||||
9° | 49° | 89° | 129° | 169° | 209° | 249° | 289° | 329° | |||||||||||||||||
10° | 50° | 90° | 130° | 170° | 210° | 250° | 290° | 330° | |||||||||||||||||
11° | 51° | 91° | 131° | 171° | 211° | 251° | 291° | 331° | |||||||||||||||||
12° | 52° | 92° | 132° | 172° | 212° | 252° | 292° | 332° | |||||||||||||||||
13° | 53° | 93° | 133° | 173° | 213° | 253° | 293° | 333° | |||||||||||||||||
14° | 54° | 94° | 134° | 174° | 214° | 254° | 294° | 334° | |||||||||||||||||
15° | 55° | 95° | 135° | 175° | 215° | 255° | 295° | 335° | |||||||||||||||||
16° | 56° | 96° | 136° | 176° | 216° | 256° | 296° | 336° | |||||||||||||||||
17° | 57° | 97° | 137° | 177° | 217° | 257° | 297° | 337° | |||||||||||||||||
18° | 58° | 98° | 138° | 178° | 218° | 258° | 298° | 338° | |||||||||||||||||
19° | 59° | 99° | 139° | 179° | 219° | 259° | 299° | 339° | |||||||||||||||||
20° | 60° | 100° | 140° | 180° | 220° | 260° | 300° | 340° | |||||||||||||||||
21° | 61° | 101° | 141° | 181° | 221° | 261° | 301° | 341° | |||||||||||||||||
22° | 62° | 102° | 142° | 182° | 222° | 262° | 302° | 342° | |||||||||||||||||
23° | 63° | 103° | 143° | 183° | 223° | 263° | 303° | 343° | |||||||||||||||||
24° | 64° | 104° | 144° | 184° | 224° | 264° | 304° | 344° | |||||||||||||||||
25° | 65° | 105° | 145° | 185° | 225° | 265° | 305° | 345° | |||||||||||||||||
26° | 66° | 106° | 146° | 186° | 226° | 266° | 306° | 346° | |||||||||||||||||
27° | 67° | 107° | 147° | 187° | 227° | 267° | 307° | 347° | |||||||||||||||||
28° | 68° | 108° | 148° | 188° | 228° | 268° | 308° | 348° | |||||||||||||||||
29° | 69° | 109° | 149° | 189° | 229° | 269° | 309° | 349° | |||||||||||||||||
30° | 70° | 110° | 150° | 190° | 230° | 270° | 310° | 350° | |||||||||||||||||
31° | 71° | 111° | 151° | 191° | 231° | 271° | 311° | 351° | |||||||||||||||||
32° | 72° | 112° | 152° | 192° | 232° | 272° | 312° | 352° | |||||||||||||||||
33° | 73° | 113° | 153° | 193° | 233° | 273° | 313° | 353° | |||||||||||||||||
34° | 74° | 114° | 154° | 194° | 234° | 274° | 314° | 354° | |||||||||||||||||
35° | 75° | 115° | 155° | 195° | 235° | 275° | 315° | 355° | |||||||||||||||||
36° | 76° | 116° | 156° | 196° | 236° | 276° | 316° | 356° | |||||||||||||||||
37° | 77° | 117° | 157° | 197° | 237° | 277° | 317° | 357° | |||||||||||||||||
38° | 78° | 118° | 158° | 198° | 238° | 278° | 318° | 358° | |||||||||||||||||
39° | 79° | 119° | 159° | 199° | 239° | 279° | 319° | 359° |
Ending Angle | Length | ||
3 | 4 | 2 | |
23 | 28 | 6 | |
31 | 37 | 7 | |
44 | 48 | 5 | |
56 | 60 | 5 | |
64 | 71 | 8 | |
74 | 76 | 3 | |
88 | 91 | 4 | |
94 | 96 | 3 | |
99 | 104 | 6 | |
110 | 115 | 6 | |
131 | 134 | 4 | |
138 | 154 | 17 | |
173 | 181 | 9 | |
186 | 187 | 2 | |
220 | 238 | 19 | |
242 | 246 | 5 | |
273 | 279 | 7 | |
286 | 289 | 4 | |
307 | 360 | 54 |
Two-dimensional Gray codes are used in communication to minimize the number of bit errors in quadrature amplitude modulation (QAM) adjacent points in the constellation. In a typical encoding the horizontal and vertical adjacent constellation points differ by a single bit, and diagonal adjacent points differ by 2 bits.
Two-dimensional Gray codes also have uses in location identifications schemes, where the code would be applied to area maps such as a Mercator projection of the earth's surface and an appropriate cyclic two-dimensional distance function such as the Mannheim metric be used to calculate the distance between two encoded locations, thereby combining the characteristics of the Hamming distance with the cyclic continuation of a Mercator projection.
If a subsection of a specific codevalue is extracted from that value, for example the last 3 bits of a 4-bit gray-code, the resulting code will be an "excess gray code". This code shows the property of counting backwards in those extracted bits if the original value is further increased. Reason for this is that gray-encoded values do not show the behaviour of overflow, known from classic binary encoding, when increasing past the "highest" value.
Example: The highest 3-bit gray code, 7, is encoded as (0)100. Adding 1 results in number 8, encoded in gray as 1100. The last 3 bits do not overflow and count backwards if you further increase the original 4 bit code.
When working with sensors that output multiple, gray-encoded values in a serial fashion, one should therefore pay attention whether the sensor produces those multiple values encoded in 1 single gray-code or as separate ones, as otherwise the values might appear to be counting backwards when an "overflow" is expected.
2 | |
Z | |
2 |
Z4
2m | |
Z | |
2 |
m | |
Z | |
4 |
2 | |
Z | |
2 |
Z4
There are a number of binary codes similar to Gray codes, including:
The following binary-coded decimal (BCD) codes are Gray code variants as well:
Name | Bit | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Tracks | Cyclic | 5s | Comment | |||
Gray BCD | 4 | 0—3 | 4 (3) | No | (2, 4, 8, 16) | No | ||||||||||||
3 | ||||||||||||||||||
2 | ||||||||||||||||||
1 | ||||||||||||||||||
Paul | 4 | 1—3 | 4 (3) | No | 2, 10 | No | ||||||||||||
3 | ||||||||||||||||||
2 | ||||||||||||||||||
1 | ||||||||||||||||||
Glixon | 4 | 0—3 | 4 | No | 2, 4, 8, 10 | (shifted +1) | ||||||||||||
3 | ||||||||||||||||||
2 | ||||||||||||||||||
1 | ||||||||||||||||||
Tompkins I | 4 | 0—4 | 2 | No | 2, 4, 10 | Yes | ||||||||||||
3 | ||||||||||||||||||
2 | ||||||||||||||||||
1 | ||||||||||||||||||
O'Brien I (Watts) | 4 | 0—3 | 4 | 9 | 2, 4, 10 | Yes | ||||||||||||
3 | ||||||||||||||||||
2 | ||||||||||||||||||
1 | ||||||||||||||||||
Petherick (RAE) | 4 | 1—3 | 3 | 9 | 2, 10 | Yes | ||||||||||||
3 | ||||||||||||||||||
2 | ||||||||||||||||||
1 | ||||||||||||||||||
O'Brien II | 4 | 1—3 | 3 | 9 | 2, 10 | Yes | ||||||||||||
3 | ||||||||||||||||||
2 | ||||||||||||||||||
1 | ||||||||||||||||||
Susskind< | -- I--> | 4 | 1—4 | 3 | 9 | 2, 10 | Yes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
3 | ||||||||||||||||||
2 | ||||||||||||||||||
1 | ||||||||||||||||||
Klar | 4 | 0—4 | 4 (3) | 9 | 2, 10 | Yes | ||||||||||||
3 | ||||||||||||||||||
2 | ||||||||||||||||||
1 | ||||||||||||||||||
Tompkins II | 4 | 1—3 | 2 | 9 | 2, 10 | Yes | ||||||||||||
3 | ||||||||||||||||||
2 | ||||||||||||||||||
1 | ||||||||||||||||||
4 | 1—4 | 4 | 9 | 2, 10 | Yes | |||||||||||||
3 | ||||||||||||||||||
2 | ||||||||||||||||||
1 |