将两个 3 字节整数和一个 2 字节整数合并为一个 8 字节整数

Combine two 3 byte integers, and one 2 byte integer into one 8 byte integer

尝试将三个整数存储为一个以用于散列,并解码回它们的原始值。

变量:

x = 3 byte integer (Can be negative)
z = 3 byte integer (Can be negative)
y = 2 byte integer (Cannot be negative)

我当前的代码 - 不适用于底片:

long combined = (y) | (((long) z) << 16) | ((((long) x)) << 40);
int newX = (int) (combined >> 40); // Trim off 40 bits, leaving the heading 24
int newZ = (int) ((combined << 24) >> (40)); // Trim off 24 bits left, and the 16 bits to the right
int newY = (int) ((combined << 48) >> 48); // Trim off all bits other then the first 16

它不适用于负数,因为您的“3 字节整数”或“2 字节整数”实际上是一个常规的 4 字节整数。如果数字为负数,所有最高位将被设置为“1”;如果你将二进制或数字放在一起,这些高 1 位将覆盖其他数字的位。

您可以使用位掩码对数字进行正确编码:

long combined = (y & 0xffff) | (((long) z & 0xffffff) << 16) | ((((long) x & 0xffffff)) << 40);

这将切断您感兴趣的 16 或 24 位范围之外的高位。

解码已经工作正常,因为您执行的位移处理了符号扩展。