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.
Read 4 bits number stored into a byte
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement