Advertisement

Read 4 bits number stored into a byte

Started by February 07, 2016 08:46 PM
1 comment, last by jor1980 8 years, 10 months ago

Hi. I am reading a file with a java code, the file has stored into 4 bits of a byte a number that represents the header size. I don´t know how to extract the number represented in those 4 bits. In my example I have the byte 0x80 that must represents a header size of 32.

There are >> and << operations to shift bits, and the & operation to mask out unwanted bits.

Say you want the 4 upper bits? (You didn't tell which ones you need, but bit 7 is the only '1' in 0x80, so I assume that is part of the number?)


int byte = 0x80;
int hi = byte >> 4;  // Shift 4 bits to the right (the upper 4 bits got shifted to the lower 4 bits)
hi = hi & 0x0F;      // Mask everything but the lower 4 bits to 0, so only the 4 lower bits remain.

now, 0x80 becomes 0x08 with this operation, not sure how to arrive at 32 without knowing how to interpret the number.

Maybe it's the number of longs? (32 bit integers), 8*4 is at least 32.

Advertisement

I think you are right it must be the number of longs. Thank you very much because I was lost

This topic is closed to new replies.

Advertisement