dividing system.collections.bitarray into sub bitarrays of 32 bits each

前端 未结 2 542
执念已碎
执念已碎 2021-01-25 03:34

I have searched in net but not getting exactly what I need. I have a bitarray of size 15,936. I need to divide this bit array into list of bitarrays , with each bit array having

2条回答
  •  北海茫月
    2021-01-25 04:15

    You can copy your bit array into an array of bytes, split that array into chunks and create new bit arrays:

    const int subArraySizeBits = 32;
    const int subArraySizeBytes = subArraySizeBits / 8;
    byte[] bitData = new byte[myBitArray.Length / subArraySizeBytes];
    myBitArray.CopyTo(bitData, 0);
    List result = new List();
    for (int index = 0; index < bitData.Length; index += subArraySizeBytes) {
        byte[] subData = new byte[subArraySizeBytes];
        Array.Copy(bitData, index * subArraySizeBytes, subData, 0, subArraySizeBytes);
        result.Add(new BitArray(subData));
    }
    

提交回复
热议问题