直接上代码
/*
long GetIndex(long w, long x, long y, long Step = 8)
{
long xi = (x - x % Step) / Step;
long dx = x % Step;
return (y - 1) * w + xi;
}
/*
获取一个位图索引内某一位的值
位图索引为 宽为w, 实际代表w*step 宽,一byte 存8个索引值
x, y为实际 位置
index = (y - 1) * w + xi; 为计算出来的在位图索引内的位置
*/
unsigned char GetIndexValue(unsigned char *pBuffer, long w, long x, long y, long Step = 8)
{
int index = GetIndex(w, x, y);
return (pBuffer[index] & (1 << (x % Step)));
}
//取某一位
unsigned char bit_get(unsigned char p_data, unsigned char position)
{
return (p_data & (1 << position));
}
//设置某一位的值为1或者0
void bit_set(unsigned char* p_data, unsigned char position, int flag)
{
//assert(position >= 0 && position <=8);
if (flag == 1)
{
*p_data |= (1 << (position - 1));
}
else if (flag == 0)
{
*p_data &= ~(1 << (position - 1));
}
}