Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Counting in Gray code.
Replies: 18   Last Post: Jun 17, 2000 4:27 PM

 Messages: [ Previous | Next ]
 eppstein@euclid.ics.uci.edu Posts: 128 Registered: 12/8/04
Re: Counting in Gray code.
Posted: Jun 15, 2000 2:29 AM

> <hermann.kremer@online.de> wrote:
> >For a given Gray code g > 0; its inverse is
> > n = invgray(g) = g XOR [g/2] XOR [g/4] XOR ...

> If this is easy to program in C++ am I right in thinking converting
> Gray back to standard binary can also be done with shift registers?

If you really want to do it in C++, try

inline unsigned long unGray(unsigned long r) {
r ^= r>>1;
r ^= r>>2;
r ^= r>>4;
r ^= r>>8;
r ^= r>>16;
return r;
}

5 shifts and xors instead of 31.

(I took this from a program I wrote to display properties of cellular
automaton rules -- see e.g. http://www.ics.uci.edu/~eppstein/ca/map.html --
since gray-code-adjacent rules are much more likely to be similar to
--
David Eppstein UC Irvine Dept. of Information & Computer Science
eppstein@ics.uci.edu http://www.ics.uci.edu/~eppstein/

Date Subject Author
6/13/00 Pertti Lounesto
6/15/00 Wim
6/13/00 Hugo van der Sanden
6/14/00 Hugo van der Sanden
6/13/00 Hermann Kremer