2011-11-03 58 views
0

我有下一个问题,我有java中的整数和从0到29的咬伤是时间戳,从30到31的位表示水平(可能的值为0,1,2,3)。所以我的问题是,我如何从这个整数中得到时间戳作为long值,以及如何从这个整数中得到字节级别的字节。拆分java整数并得到值

回答

0

这里是正确的答案:

void extract(int input) { 
     int level = input >>> 30; 
     int timestamp = (input & ~0xC0000000); 
    } 

由于先前的家伙,他们的答案。

1
int value = ...; 
int level = value & 0x3; 
long timestamp = (long) ((value & ~0x3) >>> 2); 
+0

Ooops,'&〜0x3'有点多余。 –

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

+0

这应该是'&0x03',而不是'&& 0x03'。 –

+0

呼叫良好。更新。 –