Search code examples
splitintegerbitoperation

Split java integer and get values


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.


Solution

  • Here is the right answer:

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

    Thanks to previous guys to they answers.