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*!

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.

To transform binary into reflected binary (Gray code), the algorithm consists in calculating the exclusive OR (XOR) between the binary value and itself but shifted by a row to the right (the last bit is deleted).

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

The algorithm implementation in computers languages is done in one line and uses binary operators xor and shift: `function bin2gray(n) return n ^ (n >> 1)`

An algorithm for converting an integer to Gray code (binary) uses successive divisions by powers of 2 and looks at the parity of the rounded quotient. (Thanks G. Plousos)

__Example:__ $$ \begin{align} 29 / 2 = 14.5 \approx 15 & \Rightarrow 1 \\ 29 / 4 = 7.25 \approx 7 & \Rightarrow 1 \\ 29 / 8 = 3.625 \approx 4 & \Rightarrow 0 \\ 29 / 16 = 1.8125 \approx 2 & \Rightarrow 0 \\ 29 / 32 = 0.90625 \approx 1 & \Rightarrow 1 \end{align} $$ The decimal value `29` has the binary value `10011` in Gray code.

Another conversion method, more visual, is described by this image (Thanks G. Plousos) :

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: `function gray2bin(n1) {`

n2 = n1;

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

return n2;

}

Gray Code allows you to count in binary by modifying a single bit to go from one number to the next. Here are the first 16 values in 4-bit gray code:

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

The first equivalent decimal values are: 0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8, 24, 25, 27, 26, 30, 31 , 29, 28, 20, 21, 23, 22, 18, 19, 17, 16, etc. here

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

Gray code is difficult to distinguish from other binary code.

The color gray (also written *grey*) is a clue.

The Gray code is protected by a patent from 1953.

dCode retains ownership of the "Gray Code" source code. Except explicit open source licence (indicated Creative Commons / free), the "Gray Code" algorithm, the applet or snippet (converter, solver, encryption / decryption, encoding / decoding, ciphering / deciphering, breaker, translator), or the "Gray Code" functions (calculate, convert, solve, decrypt / encrypt, decipher / cipher, decode / encode, translate) written in any informatic language (Python, Java, PHP, C#, Javascript, Matlab, etc.) and all data download, script, or API access for "Gray Code" are not public, same for offline use on PC, mobile, tablet, iPhone or Android app!

Reminder : dCode is free to use.

The copy-paste of the page "Gray Code" or any of its results, is allowed (even for commercial purposes) as long as you credit dCode!

Exporting results as a .csv or .txt file is free by clicking on the *export* icon

Cite as source (bibliography):

*Gray Code* on dCode.fr [online website], retrieved on 2024-06-15,

- Code Gray Decoder
- Code Gray Encoder
- What is the gray code? (Definition)
- How to convert binary to Gray code?
- How to convert a decimal to Gray code?
- How to convert Gray code to binary?
- What are the first values in Gray Code?
- What are the advantages of Gray Code?
- How to recognize Gray Code?
- When was Gray Code invented?

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

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

© 2024 dCode — El 'kit de herramientas' definitivo para resolver todos los juegos/acertijos/geocaching/CTF.

Feedback