问题是我有一个200个索引的字节数组,只想检查MyArray [75]的第四位是零(0)还是一(1)。
byte[] MyArray; //with 200 elements
//check the fourth BIT of MyArray[75]
if((MyArray[75] & 8) > 0) // bit is on
else // bit is off
& 运算符可以将一个值用作掩码。
xxxxxxxx = ?
00001000 = 8 &
----------------
0000?000 = 0 | 8
1 = 00000001
2 = 00000010
4 = 00000100
8 = 00001000
16 = 00010000
32 = 00100000
64 = 01000000
128 = 10000000
Something like:
if ( (MyArray[75] & (1 << 3)) != 0)
{
// it was a 1
}
private bool BitCheck(byte b, int pos)
{
return (b & (1 << (pos-1))) > 0;
}
1 << (pos-1)
语句,或者是针对0索引位置的1 << pos
语句。 - maxwellb