# Is there any practical trick to mentally count in Gray code?

When I was fairly young, I taught myself to count in binary. I thought it would be a fun party trick to impress people. I soon found out that it was not.

Over the years I've come to appreciate Gray code/reflected binary code for its property of only flipping one bit for each increment/decrement of the underlying count. But I've always been bothered by the fact that, if I wanted to mentally take any arbitrary Gray code and add or subtract 1 from it, I'd have to either convert it to and then back from its numeric value, or construct a table to work out what the next code should be.

It seems to me that there should be some trick that a person with "average" short term memory and addition skills should be able to do to take any arbitrary value in Gray code and figure out which bit to flip to get the next value... But I've never found it.

Does such a thing exist?

• What research have you done? We expect you to do a significant amount of research before asking, including checking standard references (like Wikipedia, a textbook), and to show us in the question what you've found. If your question is answered in the obvious place on Wikipedia, you probably haven't done enough research before asking. There's little point in us repeating material already widely available in standard references. – D.W. Aug 25 '16 at 23:08

Wikipedia describes a very simple algorithm for this task:

To construct the binary-reflected Gray code iteratively, at step 0 start with the $\text{code}_0 = 0$, and at step $i > 0$ find the bit position of the least significant 1 in the binary representation of $i$ and flip the bit at that position in the previous code $\text{code}_{i-1}$ to get the next code $\text{code}_i$.

Gray codes are hamilton cycles in hypercube graphs $$Q_n$$, which can be recursively constructed using Hamilton cycles in $$Q_{n-1}$$. Perhaps you can do these computations by hand for small n and see if that helps.