tags:

views:

420

answers:

2

How would I go about converting a bytearray to a bit array?

+13  A: 

The obvious way; using the constructor that takes a byte array:

BitArray bits = new BitArray(arrayOfBytes);
Guffa
A: 

It depends on what you mean by "bit array"... If you mean an instance of the BitArray class, Guffa's answer should work fine.

If you actually want an array of bits, in the form of a bool[] for instance, you could do something like that :

byte[] bytes = ...
bool[] bits = bytes.SelectMany(GetBits).ToArray();

...

IEnumerable<bool> GetBits(byte b)
{
    for(int i = 0; i < 8; i++)
    {
        yield return (b & 0x80) != 0;
        b *= 2;
    }
}
Thomas Levesque