Checking if a bit is set or not

Manjoor picture Manjoor · Mar 12, 2010 · Viewed 88.3k times · Source

How to check if a certain bit in a byte is set?

bool IsBitSet(Byte b,byte nPos)
{
   return .....;
}

Answer

Mario F picture Mario F · Mar 12, 2010

sounds a bit like homework, but:

bool IsBitSet(byte b, int pos)
{
   return (b & (1 << pos)) != 0;
}

pos 0 is least significant bit, pos 7 is most.