Gray Code: Binary to Gray Code Converter

💡
Key learnings:
  • Gray Code Definition: Gray code is defined as a binary sequence where each subsequent number differs from the previous one by only one bit.
  • Binary to Gray Conversion: Converting binary to gray code starts with the MSB and uses XOR to determine subsequent bits.
  • Applications of Gray Code: Gray code is used in digital communications and analog to digital converters to minimize conversion errors.
  • BCD to Gray Code: This specific conversion is essential for digital systems that need to minimize error during digital processing.
  • Conversion Simplicity: Despite the simplicity in converting between binary and gray codes, gray code isn’t suitable for arithmetic operations.

What is Gray Code?

Gray code, also called Cyclic Code, Reflected Binary Code (RBC), Reflected Binary (RB) or Grey code, is an ordering of bit patterns in which adjacent code words differ in one bit. In the binary-reflected sequence, this property also holds between the last and first code words, making the sequence cyclic.

The one-bit transition property helps reduce ambiguity in position encoders, some converter interfaces and synchronised counters. Gray code does not correct communication errors by itself. Arithmetic circuits normally convert it to binary before calculation because Gray-code bit positions do not have ordinary binary place values.

Gray Code Table

The table maps each decimal value to its conventional binary representation and its binary-reflected Gray-code word:

Gray Code Table

Binary to Gray Code Converter

A binary-to-Gray converter maps an n-bit binary value to an n-bit binary-reflected Gray code. The recursive construction reflects the (n-1)-bit sequence after 2n-1 entries, prefixes the first half with 0 and prefixes the reflected half with 1. In logic, the same mapping is g = b XOR (b shifted right by one bit).

The four-bit conversion table is shown below:

Binary To Gray Code Converter

For a four-bit reflected code, reflect the three-bit sequence after the (24-1)th, or 8th, row. Prefix the upper half with 0 and the reflected lower half with 1.

The XOR-gate implementation of the four-bit converter is shown below:

BCD to Gray Code

How to Convert Binary to Gray Code

  1. Copy the binary MSB directly to the Gray-code MSB.
  2. For the next Gray bit, XOR the first and second binary bits. Equal bits produce 0; different bits produce 1.
  3. Continue across the word. Each remaining Gray bit is the XOR of the binary bit in the same position and the binary bit immediately to its left.

Binary to Gray Code Conversion Example

Convert binary 01001 to Gray code by copying the MSB and then XORing each adjacent pair:

  1. Copy the binary MSB, 0, to the first Gray bit.
  2. XOR the first pair, 0 and 1. They differ, so the second Gray bit is 1.
  3. XOR the next pair, 1 and 0. They differ, so the third Gray bit is 1.
  4. XOR the next pair, 0 and 0. They match, so the fourth Gray bit is 0.
  5. XOR the final pair, 0 and 1. They differ, so the fifth Gray bit is 1.
  6. The completed Gray-code word is 01101.
Gray Code Converter

Gray to Binary Code Converter

A gray to binary code converter maps a Gray-code input to the corresponding conventional binary value.

The following four-bit truth table defines every input-output pair and can be used to derive the converter logic:

Binary to Gray Code Truth Table

The resulting Gray-to-binary converter circuit is shown below:

Gray To Binary Code Converter Circuit

Gray Code to Binary Conversion

Gray code to binary conversion uses cumulative XOR from the MSB towards the LSB:

  1. Copy the Gray-code MSB to the binary MSB.
  2. For each lower position, XOR the previous binary bit with the current Gray bit. A Gray bit of 0 keeps the previous binary value; a Gray bit of 1 toggles it.
  3. Repeat the cumulative XOR through the LSB to complete the Gray code to binary conversion.
Gray Code To Binary

The following example applies this rule to Gray code 01101.

Gray Code to Binary Conversion Example

Copy the Gray MSB, 0, to the binary MSB. The next Gray bit is 1, so toggle the previous binary bit to get 1. The third Gray bit is also 1, so toggle again to get 0. The fourth Gray bit is 0, so keep the previous binary bit at 0. The fifth Gray bit is 1, so toggle to 1. Gray code 01101 therefore converts to binary 01001.

Binary To Grey
Binary To Grey Converter
4 bit binary to gray code converter
BCD To Grey Code
Grey To Binary Conversion
Binary To Grey Formula

Gray Code Example Sequence

  1. Each pair of adjacent words in the reflected Gray sequence differs in one bit. The changed position does not move in a simple fixed direction from LSB to MSB.
  2. Start the four-bit sequence at 0000. The next word, 0001, changes only the LSB and represents decimal 1.
  3. Decimal 2 is 0011 in reflected Gray code. Moving from 0001 to 0011 changes one bit, whereas moving directly to binary 0010 would change two bits.
  4. The next unused adjacent word is 0010. Only the LSB changes from 0011, so 0010 represents decimal 3.
  5. Reflecting the two-bit pattern and prefixing the new half with 1 gives 0110 for decimal 4. This recursive rule preserves one-bit adjacency and avoids repeating 0000.
  6. Continue the reflected pattern for the remaining values. Every step changes one bit, and all 16 four-bit words appear once before the sequence returns to 0000.

Label the four Gray-code outputs G4G3G2G1. The conversion table then gives the following output functions:

Gray Code

The corresponding sum-of-products expressions can be simplified with Karnaugh maps (K-maps) for G4, G3, G2, and G1.

Gray Code K Map
Binary Code K Map
Gray Map
Grey Code To Binary

Application of Gray Code

Gray code is useful where adjacent states may be sampled during a transition. Examples include absolute position encoders, some analog-to-digital converter architectures and Gray-coded read or write pointers in asynchronous FIFOs. The code limits an adjacent-state transition to one changing bit; it does not provide error detection or correction by itself.

Frank Gray filed his pulse-code communication patent in 1947, and it was issued in 1953. The patent called the sequence reflected binary code and specified that neighbouring signal levels differ in one digit. Historical Bell Labs work on related coding equipment is described in television by pulse code modulation.

Other uses of Gray code include:

  • Karnaugh-map cell ordering for Boolean simplification
  • Counter values transferred between clock domains with synchronisers
  • Mechanical and optical position encoders
  • Alternative chromosome encoding in some genetic algorithms
  • Combinatorial algorithms and mathematical puzzles
  • Structured-light pattern sequences

Advantages of Gray Code

  • Limits adjacent-state ambiguity to one changing bit in a valid sequence
  • Avoids large Hamming-distance jumps between adjacent encoded integers in some genetic algorithms
  • Provides adjacent-cell ordering for Karnaugh-map logic minimisation
  • Supports asynchronous FIFO pointer transfer when combined with synchronisers and timing constraints

Disadvantages of Gray Code

  • Requires conversion to binary for ordinary positional arithmetic
  • Needs extra conversion logic when the surrounding system uses binary values
Want To Learn Faster? 🎓
Get electrical articles delivered to your inbox every week.
No credit card required—it’s 100% free.

About Electrical4U

Electrical4U is dedicated to the teaching and sharing of all things related to electrical and electronics engineering.

Leave a Comment