private bool GetBit(byte b, int pos)
{
return ((b & (byte)(1 << pos)) != 0);
}
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Oct 14, 2014, 7:13 AM
(byte)(1 << pos) gives you a byte in which only the bit at position 'pos' is set. If you then '&' this byte with the actual byte, b, you mask off all the other bits i.e. set them to zero.
If you're left with something which is non-zero, the bit you're interested in must be set (true) but if it's zero then it isn't set (false).
Munesh SharmaPosted Oct 14, 2014, 7:04 AM
.
The leftshift operator is the equivalent of moving all the bits of a number a specified number of places to the left:
private bool GetBit(byte b, int pos)
{
return ((b & (byte)(1 << pos)) != 0);
}
here you will pass value for b, and pos
you can about this better from this link
http://www.c4learn.com/c-programming/c-bitwise-left-shift-operator/
after checking all condition it will return a value.
Wim SturkenboomPosted Oct 14, 2014, 6:56 AM
'1' 00000001
shift '1' 2 positions gives 00000100
shifted '1' 00000100
b 00000101
& --------
result 00000100
Check if result is zero; if not, return true, else return false