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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Wim SturkenboomRank #2,527 · 554 reputationPosted Oct 14, 2014, 2:56 AM EDT
'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
Munesh SharmaRank #174 · 13.9K reputationPosted Oct 14, 2014, 3:04 AM EDT
.
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.
VulpesRank #55 · 29.2K reputationPosted Oct 14, 2014, 3:13 AM EDT
(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).