Remark on "Algorithm 246: Graycode [Z]"
The following modifications to Algorithm 246 will generate Gray code for any N, with each code word being generated in a bounded amount of time. Let A be a vector of zeros and ones of length N which will be the successive code words. New code words are successively generated by reversing a single bit in A each time. Routine OUTPUT, to be supplied by the user, is called on generation of every new code word. Initially A contains all zeros. At every odd-numbered step, AEN 1 is reversed. At every even-numbered step, A[J 11 is reversed, where A[J'] is the rightmost one-bit in A. (In case J = 1, the algorithm terminates.) The positions of all the one-bits are stored in an increasing order in a stack S, from bottom to top. This helps in quickly locating J , the rightmost one-bit.