Tool to convert Gray code. Gray code, or reflected binary code, is a binary system which changes only one bit for each incrementation of one unity.

Gray Code - dCode

Tag(s) : Character Encoding, Electronics

dCode is free and its tools are a valuable help in games, maths, geocaching, puzzles and problems to solve every day!

A suggestion ? a feedback ? a bug ? an idea ? *Write to dCode*!

Tool to convert Gray code. Gray code, or reflected binary code, is a binary system which changes only one bit for each incrementation of one unity.

The **Gray code**, also called reflected binary, is a binary code having the property of modifying only one bit when a number is increased (or decreased) by one unit.

Example:

Number | Binary | Gray |
---|---|---|

0 | 0000 | 0000 |

1 | 0001 | 0001 |

2 | 0010 | 0011 |

3 | 0011 | 0010 |

4 | 0100 | 0110 |

5 | 0101 | 0111 |

6 | 0110 | 0101 |

7 | 0111 | 0100 |

8 | 1000 | 1100 |

This property can have several interesting practical applications, and the **gray code** appears in Baudot code, in Hanoi towers resolution, or position encoders.

The conversion uses the xor function to transform binary into reflected binary (**Gray code**).

The algorithm consists of calculating the exclusive OR between the binary value and itself but shifted one row to the right (the last bit is deleted).

Example: $$ \begin{align} 1 0 1 1 & \\ \oplus \rightarrow 1 0 1 & (1) \\ = 1 1 0 1 & \end{align} $$ The binary code 1011 has for value 1101 in its reflected version in **Gray code**.

The algorithm implemenation uses binary operators xor and shift: `gray = n ^ (n >> 1)`

**Gray code** conversion can be done bit by bit. Given a number $ G = {g_0,g_1,\dots,g_n} $ with $ g_i $ each of its bits, then $ B = {b_0,b_1,\dots,b_n} $ is calculated as: $$ b_0 = g_0 \\ b_1 = g_0 \oplus g_1 \\ b_2 = g_0 \oplus g_1 \oplus g_2 \\ b_n = g_0 \oplus g_1 \oplus \dots \oplus g_n $$

In **gray code**, the most significant bit ($ g_0 $, often on the left) is always the same as the binary one ($ b_0 $).

The implementation of the conversion calculation also uses the xor and shift binary operators:`n2 = n1;`

while (n1 >>= 1) n2 ^= n1;

return n2;

**Gray Code** allow to count in binary but, to increment of one, only one bit changes. Here are the 16 first characters.

0000, 0001, 0011, 0010, 0110, 0111, 0101, 0100, 1100, 1101, 1111, 1110, 1010, 1011, 1001, 1000

**Gray code** is modified only one bit at once when incrementing, with simplifies calculations and speed them up in some cases.

The **Gray code** is protected by a patent from 1953

dCode retains ownership of the source code of the script Gray Code online. Except explicit open source licence (indicated Creative Commons / free), any algorithm, applet, snippet, software (converter, solver, encryption / decryption, encoding / decoding, ciphering / deciphering, translator), or any function (convert, solve, decrypt, encrypt, decipher, cipher, decode, code, translate) written in any informatic langauge (PHP, Java, C#, Python, Javascript, Matlab, etc.) which dCode owns rights will not be released for free. To download the online Gray Code script for offline use on PC, iPhone or Android, ask for price quote on contact page !

gray,code,binary,reflected,bit,convert,grey

Source : https://www.dcode.fr/gray-code

© 2020 dCode — The ultimate 'toolkit' to solve every games / riddles / geocaches. dCode

Feedback

▲