Gray Codes

The reflected binary code, also known as the Gray code after Frank Gray, is a binary numeral system in which consecutive values differ by only one bit.  The reflected binary code was originally designed to prevent spurious output from electromechanical switches.  Today, Gray codes are widely used to facilitate error correction in digital communications such as digital terrestrial television and some cable TV systems.  Wikipedia

A six-bit Gray code represents an ordering of the 64 hexagrams such that each is transformed into the next by changing exactly one line.  It is thus an extreme example of hexagram transitions, a single-line transition pathway that traverses all 64 hexagrams.  The Gray code hexagram sequence is illustrated below, including the actual code in the left column, and the hexagram reference on the right, including the line that changes to make the next transition.  Note that the sequence is cyclic; the last hexagram changes into the first.




The original reflected binary code can be generated with a simple algorithm.  But it is not unique; many other Gray codes exist.  Here are 10 six-bit Gray codes, generated by an unscientific random walk:

000000
000100
000101
000001
000011
100011
110011
110010
010010
010000
110000
111000
111100
111101
101101
100101
110101
110100
110110
100110
000110
000111
100111
110111
111111
011111
011110
111110
111010
101010
100010
100000
100100
101100
101110
101111
001111
001110
001100
001101
001001
001011
101011
111011
111001
011001
011000
001000
101000
101001
100001
110001
010001
010101
011101
011100
010100
010110
010111
010011
011011
011010
001010
000010
000000
000001
100001
100000
100100
100101
000101
000100
001100
101100
101101
001101
001111
001110
001010
000010
010010
010011
110011
111011
111001
011001
011101
010101
010001
110001
110101
111101
111100
110100
010100
010000
110000
110010
111010
111000
101000
101001
101011
101010
101110
101111
100111
110111
010111
010110
000110
000111
000011
100011
100010
100110
110110
111110
111111
011111
011110
011100
011000
011010
011011
001011
001001
001000
000000
100000
100010
110010
110011
110001
110101
111101
101101
101001
111001
011001
011011
111011
111111
101111
100111
110111
010111
011111
011101
011100
111100
111110
101110
101010
001010
001000
011000
011010
111010
111000
101000
101100
001100
001101
000101
010101
010001
000001
001001
001011
101011
100011
100001
100101
100100
100110
110110
110100
110000
010000
010100
000100
000110
010110
011110
001110
001111
000111
000011
010011
010010
000010
000000
000100
000110
000111
000011
010011
010111
010101
000101
001101
011101
111101
111001
110001
110011
100011
100111
110111
110101
100101
101101
101111
101110
001110
001111
011111
011110
010110
110110
110100
110000
100000
101000
101010
001010
000010
010010
010000
010100
011100
001100
101100
100100
100110
100010
110010
111010
011010
011011
011001
010001
000001
100001
101001
001001
001011
101011
111011
111111
111110
111100
111000
011000
001000
000000
001000
101000
111000
110000
110100
110110
110010
100010
101010
001010
001011
001111
001110
011110
011100
111100
101100
101101
111101
111001
111011
111111
110111
110101
110001
010001
000001
001001
101001
100001
100000
100100
100101
000101
001101
001100
000100
010100
010101
010111
010110
000110
100110
100111
000111
000011
000010
010010
011010
111010
111110
101110
101111
101011
100011
110011
010011
011011
011111
011101
011001
011000
010000
000000
010000
011000
111000
110000
110001
111001
011001
001001
001101
101101
101111
101110
101100
101000
101001
101011
001011
001111
000111
010111
010101
000101
000100
000110
001110
001100
001000
001010
011010
010010
010110
010100
110100
111100
111110
111111
111011
011011
011111
011110
011100
011101
111101
110101
110111
110110
110010
111010
101010
100010
000010
000011
000001
010001
010011
110011
100011
100111
100110
100100
100101
100001
100000
000000
100000
100001
000001
000011
000111
001111
101111
101101
100101
000101
000100
010100
011100
011000
011010
001010
001000
101000
101001
111001
111011
111111
110111
100111
100110
000110
000010
100010
110010
110000
110100
100100
101100
001100
001101
011101
111101
110101
110001
010001
010101
010111
010110
110110
111110
111100
111000
111010
101010
101110
001110
011110
011111
011011
011001
001001
001011
101011
100011
110011
010011
010010
010000
000000
000001
010001
010000
010100
110100
100100
100101
000101
000100
000110
100110
100010
100000
101000
101100
111100
011100
011101
010101
110101
110001
100001
101001
111001
011001
001001
001011
001010
000010
010010
110010
110000
111000
011000
011010
011110
011111
011011
010011
110011
111011
101011
101111
111111
111101
101101
001101
001111
000111
000011
100011
100111
110111
010111
010110
110110
111110
111010
101010
101110
001110
001100
001000
000000
001000
001010
001011
000011
000001
100001
100000
110000
110010
111010
111110
111100
111000
011000
011010
011011
010011
010111
000111
001111
101111
101101
100101
000101
001101
001001
101001
101000
101100
100100
000100
001100
001110
101110
101010
101011
111011
111001
011001
010001
010000
010010
010110
000110
100110
100111
110111
110110
110100
010100
010101
011101
011100
011110
011111
111111
111101
110101
110001
110011
100011
100010
000010
000000
010000
011000
011010
111010
111110
011110
011100
011101
010101
010111
011111
011011
010011
110011
110111
100111
100101
101101
001101
000101
000100
010100
110100
100100
100000
110000
111000
101000
101010
101011
111011
111111
101111
101110
001110
001010
001000
001100
101100
111100
111101
110101
110001
111001
011001
010001
000001
001001
101001
100001
100011
000011
001011
001111
000111
000110
010110
110110
100110
100010
110010
010010
000010

Listing codes like this quickly becomes very cumbersome.  Another way to represent a code sequence is to simply list the lines that change (a transition sequence).  The original Gray code sequence then looks like this (note the multiple reflected symmetries, and the preponderance of small numbers):

1213121412131215121312141213121612131214121312151213121412131216

Below are 1000 unique transition sequences.  As they were generated by random walks, the numbers tend to be more balanced.  Also note that, when listing the changed lines like this, the changes can begin on any hexagram (not just 2 like the ones above), and the resulting sequences are all still unique.  Reversing the string also creates a unique sequence as there are no reversed duplicates on the list.  So these 1000 transition sequences can generate 128,000 (1000 × 64 × 2) unique hexagram sequences, by beginning each sequence on every hexagram as well as reversing them.

If that weren’t enough, a Gray code can be rearranged by reordering its columns.  Imagine cutting one of the codes above into six vertical strips, then rearranging the strips.  Six strips can be arranged 720 different ways, and every one of them is still a Gray code.  However, they are no longer guaranteed to be unique.  That means that up to 92,160,000 (128,000 × 720) hexagram sequences can be generated by changing the starting hexagram, reversing the sequence, and permuting the columns, of the codes below.




You can generate new hexagram sequences by changing the starting hexagram and reversing or randomly permuting the columns of the codes.  Copy and paste any of the codes above into the input field, or select one at random with selectRandomize selects a code from the list above, reverses and permutes it randomly, and chooses a starting hexagram.  (There are 92,160,000 different possibilities.)  The resulting hexagram sequence will be displayed below.

  go   
select   permute   reverse   randomize



And because I knew you wanted to see it, here is the 12-bit Gray code, which traverses all 4096 hexagram pairs.  (It occupies all 8 columns below.)