liveBook · Manning

Gray code

As explained in Optimization Algorithms: AI techniques for design, planning, and control problems

A binary numeral system in which adjacent numerical values have representations differing in exactly one bit.

Gray code, also called reflected binary code, represents adjacent numerical values with exactly one differing bit. This locality reduces the Hamming-cliff effect in binary-coded genetic algorithms, where flipping a significant bit can abruptly change the decoded solution and fitness. section 8.1

A four-bit sequence

The table maps decimal values to binary and Gray representations: 0 is 0000, 1 is 0001, 2 is 0011, and 3 is 0010. Each successive Gray representation changes one bit, unlike ordinary binary around transitions such as 3 to 4. section 8.1

Table 8.1 Decimal, binary, and Gray coding (view table figure)

Decimal number

Binary code

Gray code

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

9

1001

1101

10

1010

1111

11

1011

1110

12

1100

1010

13

1101

1011

14

1110

1001

15

1111

1000

Compare decimal values with their four-bit binary and Gray-code representations from 0 through 15. section 8.1

Gray coding is useful when representation changes should produce gentler solution changes:

  • Use it in binary-coded genetic algorithms to smooth the genotype-to-phenotype mapping and reduce disruptive crossover or mutation effects. section 8.1
  • Convert four-bit binary values with XOR gates; XOR returns 1 for different inputs and 0 for matching inputs. section 8.1
  • Apply the representation in rotary encoders and digital-to-analog converters, where minimizing conversion errors matters. section 8.1
  • Treat Gray code as an alternative to binary or real-valued encoding when binary representation creates poor convergence or inefficient exploration and exploitation. section 8.9

How Optimization Algorithms: AI techniques for design, planning, and control problems covers it

How deep it goes
Explains adjacency, Hamming cliffs, four-bit examples, and XOR conversion, but does not detail a full genetic-algorithm implementation.
Examples
Uses a 0–15 binary-to-Gray table, a ticket-pricing conversion, XOR gates, rotary encoders, and digital-to-analog converters.
What it assumes
Assumes binary strings, bit changes, XOR behavior, and basic genetic-algorithm operations such as crossover and mutation.
What you can do after
Evaluate encoding choices and explain how Gray coding can smooth binary genetic-algorithm search and reduce conversion errors.
How it connects
Connects Gray code with genetic algorithms, binary encoding, Hamming distance, crossover, mutation, and real-valued encoding.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Optimization Algorithms: AI techniques for design, planning, and control problems. The section links point to the parts of the book it draws on.