Search code examples
c#bytebitbitarray

How can I convert byte to fixed size (size = 8) bitArray


I want Convert Byte to fixed size (size = 8) bit array

Want Behavior:

var bits = GetBits(0x00); // returned [0,0,0,0,0,0,0,0]

bits = GetBits(0x01); // returned [1,0,0,0,0,0,0,0]

bits = GetBits(0x0A); // returned [0,1,0,1,0,0,0,0]

I used below code but it didn't return what I want.

BitArray bits = new BitArray(byte);

Solution

  • You're using the wrong constructor.

    Try this:

    BitArray bits = new BitArray(new byte[] { 12 });
    

    BitArray

    The constructor you're using is saying how long you want the bit array to be.