How to read a single BIT in an Array of Bytes?

.net, arrays, bit, byte, c#

Solution

The fourth bit in element 75?

if((MyArray[75] & 8) > 0) // bit is on
else // bit is off

The & operator allows you to use a value as a mask.

xxxxxxxx = ?
00001000 = 8 &
----------------
0000?000 = 0 | 8

You can use this method to gather any of the bit values using the same technique.

1   = 00000001
2   = 00000010
4   = 00000100
8   = 00001000
16  = 00010000
32  = 00100000
64  = 01000000
128 = 10000000

Problem

The problem is that I have an Array of Byte with 200 Indexes and just want to check that is the Fourth bit of MyArray[75] is Zero(0) or One(1). ``` byte[] MyArray; //with 200 elements //check the fourth BIT of MyArray[75] ```

Original source