data warehouses. Given the variety of applications, it is useful to have algorithms to map in both directions. In many languages, these are better if implemented with iteration rather than recursion. The following C code performs the mappings in both directions, using iteration and bit operations rather than recursion. It assumes a square divided into n by n cells, for n a power of 2, with integer coordinates, with (0,0) in the lower left corner, (n − 1, n − 1) in the upper right corner, and a distance d that starts at 0 in the lower left corner and goes to in the lower-right corner. This is different from the animation shown above where the curve starts from upper left corner and terminates at upper right corner. //convert (x,y) to d int xy2d (int n, int x, int y) { int rx, ry, s, d=0; for (s=n/2; s>0; s/=2) { rx = (x & s) > 0; ry = (y & s) > 0; d += s * s * ((3 * rx) ^ ry); rot(n, &x, &y, rx, ry); } return d; } //convert d to (x,y) void d2xy(int n, int d, int *x, int *y) { int rx, ry, s, t=d; *x = *y = 0; for (s=1; s<n; s*=2) { rx = 1 & (t/2); ry = 1 & (t ^ rx); rot(s, x, y, rx, ry); *x += s * rx; *y += s * ry; t /= 4; } } //rotate/flip a quadrant appropriately void rot(int n, int *x, int *y, int rx, int ry) { if (ry == 0) { if (rx == 1) { *x = n-1 - *x; *y = n-1 - *y; } //Swap x and y int t = *x; *x = *y; *y = t; } } These use the C conventions: the & symbol is a bitwise AND, the ^ symbol is a bitwise XOR, += is the addition/assignment operator (x += y is equivalent to x = x + y), and /= is the division/assignment operator. The handling of booleans in C means that in xy2d, the variable rx is set to 0 or 1 to match bit s of x, and similarly for ry. The xy2d function works top down, starting with the most significant bits of x and y, and building up the most significant bits of d first. The d2xy function works in