Jump to content
  • Advertisement
Sign in to follow this  
jor1980

Read 4 bits number stored into a byte

This topic is 985 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
Advertisement

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!