I am trying to conver Bitarray into an array of corresponding decimal values if the bit is 1. The code I write is below but i don't want to use for loop. Is there a way around to avoid loop?
int right = 15;
var b = new BitArray(new int[] { right });
var valueList = new List<int>();
for (int i = 0; i < rightList.Count; i++)
{
if (b[i])
{
valueList.Add((int)Math.Pow(2, i));
}
}
Try copying the bits into an integer array directly.
The resulting array contains the values you want.
You can create some extension methods to get the list of integers, or the list of bytes if you want.
with some sample usage