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 formula_6 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) {  //convert d to (x,y) void d2xy(int n, int d, int *x, int *y) {  //rotate/flip a quadrant appropriately void rot(int n, int *x, int *y, int rx, int ry) {  These use the C conventions: the & symbol is a bitwise AND, the ^ symbol is a bitwise XOR, the += operator adds on to a variable, and the /= operator divides a variable. 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 the opposite order, starting with the least significant bits of "d", and building up "x" and "y" starting with the least significant bits. Both functions use the rotation function to rotate and flip the ("x","y") coordinate system appropriately. The two mapping algorithms work in similar ways. The entire square is viewed as composed of 4 regions, arranged 2 by 2. Each region is composed of 4 smaller regions, and so on, for a number of levels. At level "s", each region is "s" by "s" cells. There is a single FOR loop that iterates through levels. On each iteration, an amount is added to "d" or to "x" and "y", determined by which of the 4 regions it is in at the current level.