Bitwise mask operations to write targa file
Hello. I did write some code to write a targa file in the disk.
The code I came up is this:
Code:
ofs.put (width & 0xFF), ofs.put ((width >> 8) & 0xFF);
ofs.put (height & 0xFF), ofs.put ((width >> 8) & 0xFF);
Explanation: to write the less significant bits I pick up the integer variables and do a bitwise '&' with FF or 256, to try to preserve the less sginicant 8bits of the integer variable and discharging the most significant ones. Howerver this did not work.
Why Doesn't the compiler put a bunch of zeros to the left of the FF? If not, why? Can't he imagine that if am doing a bitwise with something larger than FF, everything to the left should be all zeros?
Now, the right side, My idea was to get rid of the eight less significant bits, or basically to put the bits 8-15 in place of 0-7 and then doing bitwise & with FF to preserve them for the write. But this doen't work either.
Can someone shed some light on this matter?