我想将一个 int 转换为代表该 int 的 2 个字节。
可能必须使用按位和位移位,但我不知道该怎么做。
int x; /* val to convert */
// ?????????
int b12; /* the first 2 bytes */
int b34; /* the last 2 bytes */
最佳答案
// Shift 16 bits to the right.
int b12 = x >>> 16;
// Zeroes the upper 16 bits.
int b34 = x & 0xffff;
关于Java 从 int var 获取第一个和最后一个 2 字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7047184/