1
public static int getIntegerFromBitArray(BitArray bitArray)
{
  var result = new int[1];
  bitArray.CopyTo(result, 0);
  return result[0];
}

// Input  A) 01110
// Output A) 14
// Input  B) 0011
// Output B) 12 <=== ????? WHY!!! :)

Can some one please explain me why my second return value is 12 instead of 3?? Please ... Thank you.

1
  • Please show us how did you use this method, how was Input A & b built. Commented Mar 14, 2012 at 7:37

2 Answers 2

10

Basically it's considering the bits in the opposite order to the way you were expecting - you haven't shown how you're mapping your input binary to a BitArray, but the result is treating it as 1100 rather than 0011.

The documentation isn't clear, admittedly, but it does work the way I'd expect it to: bitArray[0] represents the least significant value, just as it usually is when discussing binary (so bit 0 is 0/1, bit 1 is 0/2, bit 2 is 0/4, bit 3 is 0/8 etc). For example:

using System;
using System.Collections;

class Program
{
    static void Main(string[] args)
    {
        BitArray bits = new BitArray(8);
        bits[0] = false;
        bits[1] = true;

        int[] array = new int[1];
        bits.CopyTo(array, 0);
        Console.WriteLine(array[0]); // Prints 2
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

The first input(01110) is symmetric, so he didn't not notice... +1
The order actually makes perfect sense once you try to implement a function like CopyTo.
1

You need rotate bit's to right direction to get right results. 1100 is 12

4 Comments

No, rotation isn't what's required here - it's reflection. For example, if the OP's input were 1101, the result would be 11 (8 + 2 + 1), and you can't get that by rotating a bit pattern of 1101...
Yes, you are right. I mean rotate by 180 ;). Reflection is most appropriate here. if 1101 rotate by 180 we get 1011. 11 ^10 = 1011 ^2
It's not clear whether you're being flippant or not, but just in case you're not, "rotate by 180" would normally mean "rotate through 180 bits" - I would never use the term "rotate" for "reflect".
Reflection is most appropriate here.

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.