将两个 int 保存为一个 int 指定如下:
The bottom byte contains how many sectors the chunk uses. The other 3 bytes are containing the offset to the chunk-sector.
所以如果我想提取我的两个数字,我必须这样做
int usedSectors = num & 0xFF;
int sectorOffset = num >> 8;
但是我怎样才能将两个整数保存到一个整数中,例如我已经给出了usedSectors和sectorOffset(我们假设usedSectors的范围只有从0到255的1个字节和从0到16777215的3个字节的sectorOffset)?
最佳答案
测试程序:
public class ByteTest {
public static void main(String[] args) {
int expectedUsedSectors = 4;
int expectedSectorOffset = 20000;
int num = expectedSectorOffset;
num = num << 8;
num |= expectedUsedSectors;
int usedSectors = num & 0xFF;
int sectorOffset = num >> 8;
System.out.println("used sectors expected == actual? " + (expectedUsedSectors == usedSectors));
System.out.println("offset expected == actual? " + (expectedSectorOffset == sectorOffset));
}
}
首先,我设置 3 字节值,然后移位 8 位,然后设置第二个数字。
关于java - 将两个整数保存为一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31770083/