Public Group

Read 4 bits number stored into a byte

This topic is 893 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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.

Share on other sites

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.

Share on other sites

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

1. 1
Rutin
26
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• Forum Statistics

• Total Topics
631757
• Total Posts
3002135
×