Reputation: 1070
I have this BitArray that contains the following:
byte[] myBytes = {0, 0, 2, 176, 168, 3, 19, 0};
BitArray myBitArr = new BitArray(myBytes);
The result is some how revered on the disp:(EDIT: THIS IS NOT THE ISSUE)
00000000 00000000 01000000 00001101 00010101 11000000 11001000 00000000
reveres, is:
00000000 000000**00 00000010 1011**0000 10101000 00000011 00010011 00000000
To get the bits out from the myBitArr
I use the following
bool[] myNumbers = GetBitSBiArray(myBitArr, 36, 50);
with this helping method
private static bool[] GetBitSBiArray(BitArray ba, int from, int to)
{
bool[] temp = new bool[to - from];
int t = 0;
for (int i = ba.Length - to; i < ba.Length - from; i++)
{
temp[t] = ba.Get(i);
t++;
}
return temp;
}
the method above returns somehow the wrong result:
00010000 000000 (14bit)
the correct result is:
00000000 101011 (14bit) or 43
i'm not interested in overflow or other exceptions for now.
what is wrong with my method and what alternatives do i have?
Upvotes: 2
Views: 1151
Reputation: 63772
The problem is, you get all caught up in all those mysterious reversals. The format of BitArray
's byte[]
doesn't fit your format of byte[]
, and it's biting you.
It seems that your interpretation of the data is "highest bit has index 0", lowest bit h. What you need to map into is "highest bit is right, and every individual byte is lower endian".
I'd suggest getting rid of that helper code entirely. The problem is that you're using the wrong format for initializing the BitArray
- the obvious solution is to fix up the input, rather than creating helper methods to "remap" the indexing on each access.
The simplest way to get the result you want is
BitArray myBitArr = new BitArray(myBytes.Reverse().ToArray());
and the method
private static bool[] GetBitSBiArray(BitArray ba, int from, int to)
{
bool[] temp = new bool[to - from];
int t = 0;
for (int i = from; i < to; i++)
{
temp[temp.Length - t - 1] = ba.Get(i);
t++;
}
return temp;
}
The idea is that Reverse
ing the byte array will bring the bits in the individual bytes in line (that is, you get rid of the "reversed" order in each separate byte), and it will flip the order of bits, all in one operation.
The extra reversal with the temp[temp.Length - t - 1]
on output is to match the order in your sample - I assume it's actually unnecessary, since that's your manual rendering of the bits, rather than the order you want to use them in. If you simply use temp[t]
, the first bool
will correspond to bit 36, and the last to bit 50. This also means that you probably don't need to use your helper function anymore - simply use bitArray.Get(36)
to get bit 36.
Upvotes: 2
Reputation: 190
Actually I don't know how you counted the bits and how bits [36,50] are going to be "00000000 101011". But here is my answer: I would write such a function this way:
private static bool[] GetBitSBiArray(BitArray ba, int from, int to)
{
//from and to are zero-based
//you can change it the way you prefer.
if (to < from || to >= ba.Length)
return null;
List<bool> temp = new List<bool>();
int t = 0;
for (int i = from; i < to; i++)
temp.Add(ba.Get(i));
return temp.ToArray();
}
Upvotes: 0