The D-hypercube has 2^D vertices that are the D-bit binary words. A "gray code" is a hamiltonian tour on those 2^D vertices using edges to nearest neighbors only, i.e. each gray word differs from predecessor at a single bit. There are many gray codes when D is large, but some are nicer and easier to deal with than others. A Hilbert curve is a space filling curve filling the D-hypercube at unit density i.e. maps the real interval [0,1] into the cube continuously. To do it, partition [0,1] into 2^D equal subintervals, each is mapped into one of the 2^D corner half-size subcubes via a re-scaled Hilbert curve. The subcubes are ordered in gray code order. The key is to rotate and/or flip each subcube appropriately (by permuting and/or negating the D coordinates) so all the little half hilberts stitch together to make a full hilbert. OK, understand now? Now to make good elegant efficient program for this, it would if we had a fast simple routine to compute the Nth gray-word and which bit changes to reach its successor (or predecessor). I'm rather ignorant about grayness, but google found the following brilliant: uint gray(uint N){ return( N ^ (N >> 1) ); } will return the Nth gray code word! To translate that C code into english: unsigned integer gray(unsigned integer N){ return( N XOR RightShift(N) ); } I believe in a recent mathfun post it was explained how to produce the inverse of this function in O(log(D)) runtime... The goal is good hilbert code that fits on 1 page or less. It is desirable to have inverse routine too, i.e. inputs D coordinates and outputs t. The Peano-Smith curve seems way simpler than Hilbert and also basically unique (unlike hilbert which because gray codes are very non-unique, are too). But Peano-Smith naturally lives in a hyper-rectangle with edge lengths 3^(k/D) for k=1..D, not a hypercube. Needless to say, the appropriate name for D-dimensional hilbert is "Dilbert." -- Warren D. Smith http://RangeVoting.org <-- add your endorsement (by clicking "endorse" as 1st step)