Sign in to follow this  

Read 4 bits number stored into a byte

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this