Converting C# byte to BitArray

Shamim Hafiz picture Shamim Hafiz · Jun 26, 2012 · Viewed 59.6k times · Source

Is there any predefined function available to convert a byte into BitArray?

One way would be to inspect every bit of the byte value and then perform bitwise operation. I was wondering if there is any way which is more straightforward than this.

Answer

CodeCaster picture CodeCaster · Jun 26, 2012

Yes, using the appropriate BitArray() constructor as described here:

var bits = new BitArray(arrayOfBytes);

You can call it with new BitArray(new byte[] { yourBite }) to create an array of one byte.