This website uses cookies. By using this site, you consent to the use of cookies. For more information, please take a look at our Privacy Policy.
Home > Wiki encyclopedia > gray code

gray code

The typical binary Gray Code (Binary Gray Code) is abbreviated as Gray code. It was named after the patent "Pulse Code Communication" published by Frank Gray (18870913-19690523) in 1953. It was originally used for communication, but now it is often used for simulation. -Digital conversion and position-in digital conversion. The French telecommunications engineer Porter (Jean-Maurice-Émile Baudot, 18450911-19030328) used the baud code in 1880 equivalent to a variant of it. An 8-element binary mechanical counter designed by George Stibitz in 1941 fits exactly the counting rule of the Gray code counter. Gray code (Gray code) used Grey Code, Gray code, Gray code, Gray code, Golay code, cyclic code, binary reflection code, minimum error code and other names, some of them are wrong, some easy Other names are confused, it is recommended not to use them.


In the encoding of a set of numbers, if any two adjacent codes differ only by one binary number, this code is called Gray Code, and because there is only one digit between the maximum number and the minimum number Different, that is, "connected end to end", so it is also called cyclic code or reflection code. In digital systems, codes are often required to change in a certain order. For example, counting up by natural numbers, if the 8421 code is used, then all four bits will change when the number 0111 changes to 1000, and in actual circuits, the change of 4 bits cannot happen at the same time, then other codes may appear briefly in the count (1100, 1111, etc.). Under certain circumstances, it may cause circuit state errors or input errors. Using Gray code can avoid this kind of error. Gray codes have multiple encoding forms.

Gray Code (Gray Code) used names such as Grey Code, Gray Code, Gray Code, Golay Code, Cyclic Code, Reflective Binary Code, Minimum Error Code, etc. Some of them are wrong and some are easily confused with other names. It is recommended not to use these former names.


Gray code belongs to reliability coding, and is a coding method that minimizes errors. Because, although the natural binary code can be directly converted into an analog signal by a digital-to-analog converter, in some cases, such as when converting from decimal 3 to 4, each bit of the binary code must be changed, which can make the digital circuit produce a very Large spike current pulse. The Gray code does not have this shortcoming. When it switches between adjacent bits, only one bit changes. It greatly reduces the confusion of logic from one state to the next. Because there is only one bit difference between two code groups adjacent to this code, in the conversion of the angular displacement of the direction to the digital quantity, when the angular displacement of the direction changes slightly (which may cause the digital quantity to occur When changing, the Gray code changes only one bit, which is more reliable than other codes that change two or more bits at the same time, and can reduce the possibility of errors.

The Gray code is an absolute coding method. The typical Gray code is a single-step self-complementary code with reflection characteristics and cyclic characteristics. Its cyclic and single-step characteristics eliminate the possibility of significant errors in random fetching and its reflection. The self-complementing feature makes negation very convenient.

Because the Gray code is a variable weight code, each bit code has no fixed size, it is difficult to directly compare the size and arithmetic operations, and it cannot be directly converted into a liquid level signal. After one code conversion, it becomes a natural binary code. Then read by the host computer.

A typical Gray code is a quasi-weight code that uses an absolute encoding method, and the absolute value of its weight is 2^i-1 (set the lowest bit i=1).

The parity of the decimal number of the Gray code is the same as the parity of the number 1 in the code word.


Gray code has multiple encoding forms


Development History

The French engineer Jean-Maurice Eacute; the baud code used by mlle Baudot in 1880 is a variant of the typical Gray code.

Gray Code was proposed by Frank Gray of Bell Labs in the 1940s. It is used to avoid errors when transmitting signals using the PCM (Pusle Code Modulation) method.

Frank Gray's patent "Pulse Code Communication", which was applied for in 1947 and approved in 1953, was originally for communication, but was later used in analog-to-digital conversion.

In 1941, George Stibitz designed an 8-element Gray code counter.

Conversion method

Generate code table recursively

This method is based on the fact that the Gray code is a reflection code and is constructed using the following rules recursively:

1 Gray code has two code words

The first 2n code words in the (n+1)-bit Gray code are equal to the code words of the n-bit Gray code, written in order, with a prefix of 0

The last 2n code words in the (n+1)-bit Gray code are equal to the code words of the n-bit Gray code, written in reverse order, plus the prefix 1.


Angle sensor 

Machine tools, automobile brake system


Gray code

The system sometimes requires digital values generated by sensors to indicate the mechanical position. As shown in the conceptual diagram of the coding disk and some contacts, according to the position of the disk rotation, the contact generates a 3-bit binary code, a total of 8 such codes. The dark area in the disk is connected to the corresponding logic 1 signal source; the bright area is not connected, and the contact interprets it as a logic 0. The gray code is used to encode the bright and dark areas on the coded disc, so that there is only one digit change between consecutive code words. In this way, there is no erroneous coding due to the limited accuracy of device manufacturing, which causes the contact to turn to the boundary position.

Gray code

When simplifying the logic function, it can be done by Karnaugh map arranged by Gray code.

Nine Serial Problems

The state change of the nine-chain of intellectual toys conforms to the coding rule of Gray code, and the solution of Hanoi Tower is also related to Gray code.

Each ring in the nine-connected ring has two states up and down. If these two states are represented by 0/1, this state sequence will form a cyclic binary coding (Gray code) sequence. Therefore, the number of state changes required to solve the nine-connected problem is the decimal number 341 corresponding to the Gray code 111111111.


  • XCS05XL-5VQ100C


    FPGA Spartan-XL Family 5K Gates 238 Cells 250MHz 3.3V 100-Pin VTQFP

  • XC2C32A-4CP56C


    CPLD CoolRunner -II Family 750 Gates 32 Macro Cells 323MHz 0.18um, CMOS Technology 1.8V 56-Pin CSBGA

  • XC2C32A-4QFG32C


    CPLD CoolRunner -II Family 750 Gates 32 Macro Cells 323MHz 0.18um, CMOS Technology 1.8V 32-Pin QFN

  • XC2C32A-6CPG56C


    CPLD CoolRunner -II Family 750 Gates 32 Macro Cells 200MHz 0.18um, CMOS Technology 1.8V 56-Pin CSBGA

  • XC5210-5PQG160I


    Xilinx QFP-160

FPGA Tutorial Lattice FPGA
Need Help?


If you have any questions about the product and related issues, Please contact us.