Вопрос

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

Here is the right answer:

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

Thanks to previous guys to they answers.

Другие советы

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

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top