Setting bits...
/** Set bit in any sized bit block.
*
* @return none
*
* @param bit - Bit number.
* @param bitmap - Pointer to bitmap.
*
* @note Please note that this function does not know the size of the
* bitmap and it cannot range check the specified bit number.
*/
void SetBit( int bit, unsigned char *bitmap)
{
int n, x;
x = bit / 8; // Index to byte.
n = bit % 8; // Specific bit in byte.
bitmap[x] |= (1 << n); // Set bit.
}
Resetting bits...
/** Reset bit in any sized mask.
*
* @return None
*
* @param bit - Bit number.
* @param bitmap - Pointer to bitmap.
*
* @note Please note that this function does not know the size of the
* bitmap and it cannot range check the specified bit number.
*/
void ResetBit( int bit, unsigned char *bitmap)
{
int n, x;
x = bit / 8; // Index to byte.
n = bit % 8; // Specific bit in byte.
bitmap[x] &= (1 << n); // Reset bit.
}
Toggle bit...
/** Toggle bit in any sized bit mask.
*
* @return none
*
* @param bit - Bit number.
* @param bitmap - Pointer to bitmap.
*
* @note Please note that this function does not know the size of the
* bitmap and it cannot range check the specified bit number.
*/
void ToggleBit( int bit, unsigned char *bitmap)
{
int n, x;
x = bit / 8; // Index to byte.
n = bit % 8; // Specific bit in byte.
bitmap[x] ^= (1<<n); // Toggle bit.
}
Checking bit...
/** Checks specified bit.
*
* @return 1 if bit set else 0.
*
* @param bit - Bit number.
* @param bitmap - Pointer to bitmap.
*
* @note Please note that this function does not know the size of the
* bitmap and it cannot range check the specified bit number.
*/
int IsBitSet( int bit, const unsigned char *bitmap)
{
int n, x;
x = bit / 8; // Index to byte.
n = bit % 8; // Specific bit in byte.
// Test bit (logigal AND).
if (bitmap[x] & (1<<n))
return 1;
return 0;
}
Is bit reset...
/** Checks specified bit.
*
* @return 1 if bit reset else 0.
*
* @param bit - Bit number.
* @param bitmap - Pointer to bitmap.
*
* @note Please note that this function does not know the size of the
* bitmap and it cannot range check the specified bit number.
*/
int IsBitReset( int bit, const unsigned char *bitmap)
{
return IsBitSet( bit, bitmap) ^ 1;
}
Hope this helps...
Usefull bit twiddling hacks...