HNHacker News
TopNewBestAskShowJobs

thcormen

16 karma · joined January 31, 2017

submissionscomments
thcormen··on Fifty Shades of Gray Code
My student, Jessica Fan, and I published a paper in the Allerton Conference in October 2015 on just this question.

You can create a Gray code for exactly the numbers 0 to n-1, for any n, by the following steps: 1. Create the binary reflected Gray code for the next higher power of 2. 2. Take just the first n numbers of that binary reflected Gray code. 3. Bitwise exclusive-or each of these n numbers in the Gray code with the binary representation of floor(n/2) (i.e., n shifted right by one bit).

To create a cyclic Gray code for exactly the numbers 0 to n-1, n must be even. (It's easy to prove that you cannot get a cyclic Gray code for odd n.) 1. Generate the Gray code for 0 to n/2-1, as above. 2. Concatenate the reflection of that Gray code to itself. 3. Shift each of the n numbers you now have left by one bit. The first half gives you all the even numbers 0 to n-2, with the Gray-code property. 4. Put a 1 in the least significant bit in the second half The second half gives you all the odd numbers 1 to n-1, again with the Gray-code property.

The whole sequence gives you 0 to n-1, with the Gray-code property, and it's cyclic.

If you'd like a PDF of the paper, email me: thc@cs.dartmouth.edu.

Tom Cormen Professor of Computer Science Dartmouth College