I have next problem, I have integer in java and bites from 0 to 29 is timestamp, the bits from 30 to 31 means level (possible values 0, 1, 2, 3). So my question is, how do I get the timestamp as long value from this integer and how do I get the level as byte from this integer.
3 に答える
1
int value = ...;
int level = value & 0x3;
long timestamp = (long) ( (value & ~0x3) >>> 2 );
于 2011-11-03T14:25:34.170 に答える
0
Assuming the timestamp is unsigned:
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.
}
See http://download.oracle.com/javase/tutorial/java/nutsandbolts/op3.html
于 2011-11-03T14:27:24.213 に答える
0
Here is the right answer:
void extract(int input) {
int level = input >>> 30;
int timestamp = (input & ~0xC0000000);
}
Thanks to previous guys to they answers.
于 2011-11-04T13:46:41.413 に答える