Saturday, 31 August 2013

Simple bit setting and cleaning

Simple bit setting and cleaning

I'm coding an exercise from a book. This program should set a "bitmapped
graphics device" bits, and then check for any of them if they are 1 or 0.
The setting function was already written so I only wrote the test_bit
function, but it doesn't work. In the main() I set the first byte's first
bit to 1, so the byte is 10000000, then I want to test it: 1000000 &
10000000 == 10000000, so not null, but I still get false when I want to
print it out. What's wrong?
#include <iostream>
const int X_SIZE = 32;
const int Y_SIZE = 24;
char graphics[X_SIZE / 8][Y_SIZE];
inline void set_bit(const int x, const int y)
{
graphics[(x)/8][y] |= (0x80 >> ((x)%8));
}
inline bool test_bit(const int x, const int y)
{
if(graphics[x/8][y] & (0.80 >> ((x)%8)) != 0)
return true;
else return false;
}
void print_graphics(void) //this function simulate the bitmapped graphics
device
{
int x;
int y;
int bit;
for(y=0; y < Y_SIZE; y++)
{
for(x = 0; x < X_SIZE / 8; x++)
{
for(bit = 0x80;bit > 0; bit = (bit >> 1))
{
if((graphics[x][y] & bit) != 0)
std::cout << 'X';
else
std::cout << '.';
}
}
std::cout << '\n';
}
}
main()
{
int loc;
for (loc = 0; loc < X_SIZE; loc++)
{
set_bit(loc,loc);
}
print_graphics();
std::cout << "Bit(0,0): " << test_bit(0,0) << std::endl;
return 0;
}

No comments:

Post a Comment