2

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?

8
  • 5
    From MSDN: The Least Significant Bit of each byte represents the lowest index value: " bytes [0] & 1" represents bit 0, " bytes [0] & 2" represents bit 1, " bytes [0] & 4" represents bit 2, and so on. So yeah, it's supposed to be reversed. Commented Aug 20, 2015 at 12:52
  • Flip your thinking, and think right to left (since LSB and index 0 are on the right). Commented Aug 20, 2015 at 12:56
  • 1
    Are you sure about your correct result? As far as I understand, your method should get the bits from index 36 to 50 in reverse order, so 10101 11000000 1 and in reverse order 10000001110101. where does your Result (00000000 101011) come from? Commented Aug 20, 2015 at 13:10
  • @GuyMontag I have update the question, see if it helps, the correct answer come from me calculating by hand, and I have check and test the answer many times Commented Aug 20, 2015 at 13:17
  • What is it you're trying to do? Do you have a bitmask saved in a file or something like that? Where is the least significant bit in your data? Are the bytes in your data treated separately, or is it simply an array of bits? Commented Aug 20, 2015 at 13:45

2 Answers 2

2

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 Reverseing 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.

Sign up to request clarification or add additional context in comments.

1 Comment

I know I should t write thank you comments in here, but man, you really did understood my weird question, THX, it really did byte me
0

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();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.