What are Gray Codes and How to Extend Them?

Last Modified:

What are Gray Codes?

Gray codes are a set of codes which exhibit a unique property where adjacent values differ by only one bit.

Why are Gray Codes Important?

This essential property of Gray codes addresses several challenges in digital systems caused by binary counting, where multiple bits may change at once. By ensuring only a single-bit transition between successive values, Gray codes sacrifice computational convenience in favor of greater robustness and transition efficiency. They excel in scenarios where physical motion, power constraints, or timing precision take precedence over arithmetic operations—turning potentially unstable systems into reliable, low-power designs.

How to Generate Gray Codes?

The 1-bit Gray codes—0 and 1—are identical to their binary counterparts. To generate Gray codes with more bits, the following steps are applied recursively:

  1. Prefix 0 to each existing Gray code from the previous stage.
  2. Mirror the list of existing codes in reverse order.
  3. Prefix 1 to each code in the mirrored list.

This process ensures that each successive code differs from the previous by only one bit, preserving the minimal transition property essential to Gray code design.

2-Bit Gray Codes from 1-Bit Gray Codes

The 1-bit Gray codes—0 and 1—are identical to their binary counterparts. The total number of Gray codes in 2-bits are \(\mathsf{2^2=4}\). The initial 1-bit numbers are shown in the figure:

this image shows the initial 1-bit numbers while generating 2-bit gray codes

New numbers would fill in the positions in bit1 and bit2. First, 0 is inserted at bit2 position for all the existing gray codes.

this image shows the step 1 of extending 1-bit gray codes to 2-bit gray codes

Next, the existing codes are mirrored in bit1 position in reverse order.

this image shows the step 2 of extending 1-bit gray codes to 2-bit gray codes

Finally, 1 is added to these mirrored numbers in bit1 position to complete the 2-bit Gray codes.

this image shows the step 3 of extending 1-bit gray codes to 2-bit gray codes

This figure presents a consolidated view of the Gray code generation process, with each stage visually distinguished by color. The original Gray codes are shown in black, serving as the foundation. In the first step, highlighted in blue, a 0 is prefixed to each existing code. The second step, illustrated with pink numbers and directional arrows, involves reversing the sequence. Finally, the green numbers show prefixing a 1 to each mirrored code, and mark the completed set of newly generated Gray codes, representing the outcome of the entire transformation.

this image shows the combined steps of extending 1-bit gray codes to 2-bit gray codes

3-Bit Gray Codes from 2-Bit Gray Codes

This figure demonstrates the full procedure for generating 3-bit Gray codes from the existing 2-bit sequence, using the same color-coded scheme for clarity. The original 2-bit Gray codes are displayed in black, forming the base of the transformation. In the first step, shown in blue, each code is extended by prefixing a 0, preserving the original order. The second step, marked with pink numbers and arrows, involves reversing the original sequence to prepare for reflection. In the final step, highlighted in green, a 1 is prefixed to each reversed code, completing the 3-bit Gray code set. Together, these stages illustrate the recursive structure and symmetry inherent in Gray code generation.

this image shows the combined steps of extending 2-bit gray codes to 3-bit gray codes

Gray Codes and Their Decimal Equivalence

This chart provides a clear comparison between decimal numbers, their binary representations, and the corresponding Gray code equivalents for values ranging from 0 to 15. Each row aligns a decimal value with its standard binary form, followed by its Gray code counterpart, which differs by only one bit between successive entries. This structure highlights the key advantage of Gray codes in minimizing transition errors, improving power efficiency and in simplification of digital circuits through Karnaugh maps. By visually mapping the progression across all three formats, the chart emphasizes the systematic transformation from binary to Gray code, making it easier to understand the logic and symmetry behind the encoding process.

the image presents a reference chart for comparing the first 16 values across decimal, binary and gray codes

How to Convert from Binary to Gray Codes?

To convert a binary number into its equivalent Gray code, a simple yet elegant process is used that ensures only one bit changes at each step—minimizing transition errors in digital systems. This method begins by preserving the most significant bit (MSB) of the binary input, which becomes the first bit of the Gray code. Then, each subsequent Gray bit is derived by performing a bitwise XOR between the current binary bit and its immediate predecessor. This stepwise transformation continues across the entire binary sequence, producing a Gray code.

  1. Copy the first bit (MSB stays the same).
  2. XOR each next bit with the one before it.
  3. Repeat until all bits are processed.

Example: Convert \(\mathsf{01010_2}\) in to Gray Code

This image illustrates the step-by-step conversion of the binary number \(\mathsf{01010_2}\) into its Gray code equivalent. It begins by preserving the most significant bit, followed by a series of XOR operations between each binary bit and its immediate predecessor. The visual layout clearly highlights each transition, making it easy to trace how the binary input transforms into the Gray code \(\mathsf{01111}\).

this image shows the steps of converting a binary number into its equivalent gray code

Example: Convert \(\mathsf{11011_2}\) in to Gray Code

This image showcases the conversion of the binary number \(\mathsf{11011}\) into its Gray code counterpart. The process begins by retaining the first bit, then applying XOR between each successive bit and its predecessor. The result—\(\mathsf{10110}\)—is visually mapped to emphasize how each bit transition reflects a change in state, not just value.

this image shows the steps of converting a binary number into its equivalent gray code

How to Convert from Gray Codes to Binary?

Converting Gray code to binary is a step-by-step process that helps decode a special type of binary numbering system. In Gray code, only one bit changes at a time between consecutive values, which reduces errors in digital systems. To convert it to standard binary, we start by copying the first bit as it is, then use a rule: each next binary bit is found by checking whether the previous binary bit and the current Gray bit are the same or different. This method ensures that the binary output reflects the cumulative changes encoded in the Gray sequence, preserving its minimal-change property while translating it into a format suitable for standard digital interpretation.

  1. Copy the first bit (MSB stays the same).
  2. XOR each next Gray code bit with the previous binary bit.
  3. Repeat until all bits are processed.

Example: Convert \(\mathsf{01111}\) in to Binary

The Gray code \(\mathsf{01111}\) converts to binary \(\mathsf{01010_2}\) through a process that starts by copying the first Gray bit directly into the binary result. From there, each subsequent binary bit is determined by comparing the previous binary bit with the current Gray bit—specifically, using an exclusive OR (XOR) operation. The result verifies correct conversion as we used the the reverse of this example for binary to Gray code conversion.

this image shows the steps of converting a gray code into its equivalent binary number

Example: Convert \(\mathsf{10110}\) in to Binary

For the Gray code \(\mathsf{10110}\), the same logic applies. The first binary bit is taken directly from the Gray code, and each following bit is calculated by XORing the previous binary bit with the current Gray bit. This step-by-step decoding reveals the binary equivalent \(\mathsf{11011_2}\), which is the number that was converted in to Gray code in the reverse example.

this image shows the steps of converting a gray code into its equivalent binary number