code: private int encode(int x, int y, int r) {
  
          int mask = (1 << r) - 1;
          int hodd = 0;
          int heven = x ^ y;
          int notx = ~x & mask;
          int noty = ~y & mask;
          int temp = notx ^ y;
  
          int v0 = 0, v1 = 0;
          for (int k = 1; k < r; k++) {
              v1 = ((v1 & heven) | ((v0 ^ noty) & temp)) >> 1;
              v0 = ((v0 & (v1 ^ notx)) | (~v0 & (v1 ^ noty))) >> 1;
          }
          hodd = (~v0 & (v1 ^ x)) | (v0 & (v1 ^ noty));
  
          return interleaveBits(hodd, heven);
      }
docs: java code adapted from C code in the paper "Encoding and decoding the Hilbert order" by Xian Lu and Gunther Schrack, published in Software: Practice and Experience Vol. 26 pp 1335-46 (1996).