# Thread: Bitwise mask operations to write targa file

1. ## 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?  Reply With Quote

2. I don't see why your code isn't working. You need to post a complete example program that demonstrates the problem.
Code:
```#include <iostream>
int main() {
unsigned width = 1234; // must be < 65536
std::cout << (width & 0xFF) << " | " << ((width >> 8) & 0xFF) << '\n';
return 0;
}```  Reply With Quote

3. Originally Posted by colt Code:
```ofs.put (width & 0xFF),  ofs.put ((width >> 8) & 0xFF);
ofs.put (height & 0xFF), ofs.put ((width >> 8) & 0xFF);```
You use width three times but height only once.

Besides, where would you expect to see "a bunch of zeros to the left of the FF"? Each of those ofs.put() calls is writing one byte of data to a file, correct? I would expect the file to contain exactly what you write to it (i.e., only 4 bytes, so no room for a bunch of zeroes).  Reply With Quote

4. Originally Posted by christop You use width three times but height only once.

Besides, where would you expect to see "a bunch of zeros to the left of the FF"? Each of those ofs.put() calls is writing one byte of data to a file, correct? I would expect the file to contain exactly what you write to it (i.e., only 4 bytes, so no room for a bunch of zeroes).
I was talking what would be used to do a bitwise '&' with the most significant bits of width and height (from bit 8 to 31). With '0xFF' being 1111 1111, I suppose that when doing the bitwise operation, there would be a bunch of zeros to do the operation with bytes 2 to 4 of height and width;

I don't see why your code isn't working. You need to post a complete example program that demonstrates the problem.
Here is:

Code:
```#include <iostream>
#include <vector>
#include <fstream>
#include <cstdlib>
#include <cmath>

using namespace std;

class Image {
public:
Image (int _width,int _height,int bpp) : width (_width), height (_height), bytes_per_pixel (bpp/8), ofs ("output.tga", ios::binary) {
ofs.put (0), ofs.put (0);
ofs.put (2);
ofs.put (0), ofs.put (0);
ofs.put (0), ofs.put (0);
ofs.put (0);
ofs.put (0), ofs.put (0);
ofs.put (0), ofs.put (0);

ofs.put (width & 0xFF), ofs.put ((width >> 8) & 0xFF00);
ofs.put (height & 0xFF), ofs.put ((height >> 8) & 0xFF00);
ofs.put (bpp), ofs.put (0);

image_array = new unsigned char [width*height*bytes_per_pixel];
}
unsigned char* get_image_array () { return image_array; }
ofstream ofs;
int width,height,bytes_per_pixel;
private:
unsigned char * image_array;
};

struct Vec3f {
Vec3f (float _x, float _y, float _z) : x(_x), y(_y), z(_z) {}
float x,y,z;
};

struct Sphere {
Sphere (Vec3f _center, float _radius) : center (_center), radius (_radius) {}
Vec3f center;
};

struct Scene {
vector <Sphere> sphere_vector;
vector <Vec3f> light_vector;
vector <Vec3f> material_vector;
};

main () {
Image image (255,255,24);
Scene scene;
scene.sphere_vector.push_back (Sphere (Vec3f (0.0,0,-20),4));
cout << scene.sphere_vector.radius << endl;
unsigned char * image_array = image.get_image_array ();
int image_ctr=0;
for (int y=0; y<image.height; y++) {
for (int x=0; x<image.width; x++) {
float t = 1000000;
//for (int z=0; z<scene.sphere_vector.size(); z++)
image_array [image_ctr++] = 102;
image_array [image_ctr++] = 101;
image_array [image_ctr++] = 100;
}
}
image.ofs.write ((char*)image_array,(image.width*image.height*image.bytes_per_pixel));
delete image_array;
image.ofs.close ();
}```  Reply With Quote

Popular pages Recent additions #### Tags for this Thread

bit operations, bitwise, ofs.put, width, write 