Sign in to follow this  

Unpacking an int

This topic is 3732 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

I'm doing a school project currently that does actions on a bmp such as widening a bitmap , mirroring it etc. When I widen a BMP, I have to change the value of the width in pixels and file length in the BMP header. I must do this all in C and cannot use any pre-defined structures that would make it easier. The machine I have to compile on is a Sparc machine and it is Big Endian. Here's how I pack the length of the bmp file...
short sResult;
int iResult;
int fileSize;
int i;

for(i = 0; i < 4; i++)
{
  sResult = bmpFile[5-i];
  iResult = iResult << 8;
  iResult = iResult | sResult; 
}
fileSize = iResult;
But I have no idea how to go about extracting bits from this variable to write back into the bmp Header file once I calculate new values for the file length and bmp width. Could someone give me a nudge in the right direction ? Thanks! --Matt

Share this post


Link to post
Share on other sites
>> shifts in the opposite direction as <<, allowing you to return things to the low-order bits.

bitwise-and (&) can be used to mask off high-order bits.

Assuming you actually understand what your existing code does, you should be quite well nudged enough now :)

Share this post


Link to post
Share on other sites
Sign in to follow this