The common operations are to set a bit, clear a bit, toggle a bit and check if a bit is set
unsigned int toggle(unsigned int x, unsigned int index)
{
x ^= 1 << index;
return x;
}
unsigned int SetBit(unsigned int x, unsigned int index)
{
unsigned int mask = 1 << index;
x = x | mask;
return x;
}
unsigned int ClearBit(unsigned int x, unsigned int index)
{
unsigned int mask = 1 << index;
x = x & ~mask;
return x;
}
bool IsBitSet(unsigned int x, unsigned int pos)
{
unsigned int mask = 1 << pos;
if ((x&mask))
{
return true;
}
return false;
}
No comments:
Post a Comment