Skip to content

Instantly share code, notes, and snippets.

@justADeni
Created May 26, 2025 21:33
Show Gist options
  • Select an option

  • Save justADeni/3687cc97a45e9b72976c5965c4d93aa7 to your computer and use it in GitHub Desktop.

Select an option

Save justADeni/3687cc97a45e9b72976c5965c4d93aa7 to your computer and use it in GitHub Desktop.
// Allows for packing coordinates ranging from -67_108_864 to 67_108_863
// on X and Z axis, and -64 to 320 on Y axis into one compact long
enum XYZ {
X,
Y,
Z;
public static long pack(int x, int y, int z) {
return ((long)(x & 0x7FFFFFF) << 36) // x: 27 bits
| ((long)(y + 64 & 0x1FF) << 27) // y: 9 bits, offset
| (z & 0x7FFFFFF); // z: 27 bits
}
public static int get(XYZ coordinate, long packed) {
return switch (coordinate) {
case X -> (int)(packed >> 36);
case Y -> (int)(((packed >> 27) & 0x1FF) - 64);
case Z -> (int)(packed << 37 >> 37);
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment