Shhow me a very simple one line example in C# to check if a given bit is set in given byte
function signature should be like
bool IsBitSet(Byte b,byte nPos)
{
return .....;
}
Shhow me a very simple one line example in C# to check if a given bit is set in given byte
function signature should be like
bool IsBitSet(Byte b,byte nPos)
{
return .....;
}
Here is the solution in words.
Left shift an integer with initial value 1 n times and then do an AND with the original byte. If the result is non-zero, bit is Set otherwise not. :)
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.
Right shift your input n bits down and mask with 1, then test whether you have 0 or 1.