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.
Function
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:
{| class="wikitable" style="text-align: center;"
|-
! 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,
Telegraphy codes
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 OEIS sequence A290772 gives the number of possible Gray sequences of length that include zero and use the minimum number of bits.
n-ary Gray code <span class="anchor" id="n-ary"></span><span class="anchor" id="ternary"></span><span class="anchor" id="pentary"></span>
{| style="border: 0; padding: 10px; float: right;"
|-
| <!-- Second table to provide spacing around the inner table, can't get it otherwise… -->
{| style="width: 150px; float: right; border: 1px solid #000; border-collapse: collapse;"
|-
| style="padding: 5px; text-align: right;" | Ternary number → ternary Gray code<br />
|}
|}
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 }. If n is not a power of 2, it is possible to construct well-balanced binary codes where the difference between two transition counts is at most 2; so that (combining both cases) every transition count is either <math>2\left\lfloor \tfrac{2^n}{2n} \right\rfloor</math> or <math>2\left\lceil \tfrac{2^n}{2n} \right\rceil</math>.
Monotonic Gray codes
Monotonic codes are useful in the theory of interconnection networks, especially for minimizing dilation for linear arrays of processors. based on previous work, was designed by etzenseep (Florian Bauer) in September 2022.
An STGC for P = 360 and n = 9 is reproduced here:
{| class="wikitable" style="text-align: center; background: #FFFFFF; border-width: 0;"
|+ Single-track Gray code for 360 positions
! Angle || Code
| rowspan="42" style="text-align: center; background: #FFFFFF; border-width: 0;" |
! Angle || Code
| rowspan="42" style="text-align: center; background: #FFFFFF; border-width: 0;" |
! Angle || Code
| rowspan="42" style="text-align: center; background: #FFFFFF; border-width: 0;" |
! Angle || Code
| rowspan="42" style="text-align: center; background: #FFFFFF; border-width: 0;" |
! Angle || Code
| rowspan="42" style="text-align: center; background: #FFFFFF; border-width: 0;" |
! Angle || Code
| rowspan="42" style="text-align: center; background: #FFFFFF; border-width: 0;" |
! Angle || Code
| rowspan="42" style="text-align: center; background: #FFFFFF; border-width: 0;" |
! Angle || Code
| rowspan="42" style="text-align: center; background: #FFFFFF; border-width: 0;" |
! Angle || Code
| rowspan="42" style="text-align: center; background: #FFFFFF; border-width: 0;" |
|-
| 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° ||
|}
{| class="wikitable" style="text-align: center; background: #FFFFFF; border-width: 0;"
|+ Starting and ending angles for the 20 tracks for a single-track Gray code with 9 sensors separated by 40°
|-
! Starting angle || Ending angle || Length
| rowspan="21" style="text-align: center; background: #FFFFFF; border-width: 0;" |
|-
| 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 code
200px|thumb|right|A Gray-coded constellation diagram for rectangular 16-[[Quadrature amplitude modulation|QAM]]
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.
References
</references>
Further reading
- https://web.archive.org/web/20171030140209/https://www.eetimes.com/document.asp?doc_id=1278827 --> Part 2 https://web.archive.org/web/20171030140323/https://www.eetimes.com/document.asp?doc_id=1278853 --> Part 3
- (7 pages)
- (5 pages)
- (2 pages)
External links
- "Gray Code" demonstration by Michael Schreiber, Wolfram Demonstrations Project (with Mathematica implementation). 2007.
- NIST Dictionary of Algorithms and Data Structures: Gray code.
- Hitch Hiker's Guide to Evolutionary Computation, Q21: What are Gray codes, and why are they used?, including C code to convert between binary and BRGC.
- Dragos A. Harabor uses Gray codes in a 3D digitizer.
- Single-track gray codes, binary chain codes (Lancaster 1994), and linear-feedback shift registers are all useful in finding one's absolute position on a single-track rotary encoder (or other position sensor).
- AMS Column: Gray codes
- Optical Encoder Wheel Generator
- ProtoTalk.net – Understanding Quadrature Encoding – Covers quadrature encoding in more detail with a focus on robotic applications
