我有下一个问题,我在 java 中有整数,从 0 到 29 的位是时间戳,从 30 到 31 的位表示级别(可能值为 0、1、2、3)。所以我的问题是,如何从这个整数中获取作为长值的时间戳,以及如何从这个整数中获取作为字节的级别。
问问题
659 次
3 回答
1
int value = ...;
int level = value & 0x3;
long timestamp = (long) ( (value & ~0x3) >>> 2 );
于 2011-11-03T14:25:34.170 回答
0
假设时间戳是无符号的:
void extract(int input) {
int timestamp = input >>> 2; // This takes the entire list of bits and moves drops the right 2.
int level = input & 0x03; // this takes the entire list of bits and masks off the right 2.
}
请参阅http://download.oracle.com/javase/tutorial/java/nutsandbolts/op3.html
于 2011-11-03T14:27:24.213 回答
0
这是正确的答案:
void extract(int input) {
int level = input >>> 30;
int timestamp = (input & ~0xC0000000);
}
感谢以前的人给他们的回答。
于 2011-11-04T13:46:41.413 回答