Jump to content

Whats in my BitArray?

- - - - -

  • Please log in to reply
5 replies to this topic

#1
williamevanl

williamevanl

    Learning Programmer

  • Members
  • PipPipPip
  • 61 posts
I have a bitarray that holds index values for a string so for a ')' in "abc)[def]" it would be 000100000 obviously.

I'm unit testing this function and I need to give it an expected return value but I don't know how to do that. It currently is:
BitArray Final_Result = null

so I feel like it should be something like BitArray Final_Result = (false,false,false, true, etc...) but this doesnt work. When I look at its value in the V.S its an an 8 bit integar array with the value 8.

Thanks!

#2
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
could you post the function? (or PM it if you don't want to make it public to everyone)

#3
williamevanl

williamevanl

    Learning Programmer

  • Members
  • PipPipPip
  • 61 posts
public void symbol_index_numbers(string ped, char symbol, out BitArray Final_Result_S)
        {
            string pedigree = ped;
            byte bitcount = 0;
            BitArray bitArray = new BitArray(new int[8]);
            foreach (char letter in pedigree)
            {
                if (letter == symbol)
                {
                    bitArray[bitcount] = true;
                }               
                bitcount++;
            }
            Final_Result_S = bitArray;


#4
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
so the idea is to have false for every character in the string, that isn't the symbol handed in? (let me know if I'm not answering the right question)

i'm thrown off a little why your trying to set it to new int[8], try something like this
BitArray bitArray = new BitArray(ped.Length, false);

so you would end up with a kind of mask ( FFFTFFFF )

Here is a helpful function obtained from MSDN which would help you analyze the contents of the bit array,
public static void PrintValues(IEnumerable myList, int myWidth) {
            int i = myWidth;
            foreach (Object obj in myList) {
                if (i <= 0) {
                    i = myWidth;
                    Console.WriteLine();
                }
                i--;
                Console.Write("{0,8}", obj);
            }
            Console.WriteLine();
        }

just call it like PrintValues(bit_array, 8); //this would bring out 8 values per line

#5
williamevanl

williamevanl

    Learning Programmer

  • Members
  • PipPipPip
  • 61 posts
Yes, I considered doing something like that, I mean I could just do a foreach bool in bitarray and check for true or falses or whatever. I guess my original question is how do I this without looping through each bit? So for my unit test I need does bitarray = (somevalue) I can pull out the values from my bitarray but I want to be able to compare the value of my bitarray to another variable as if bitarray has a single value, (which it does 'sort of') the bits combined in my example is 8. So I just want the unit test to check if this is the case. Does bitarrray = 8 or does bitarray = true, false, false, false or does bitarray = 1000?. How do I do that?


So my bit array has the values 0001000:
how do I write if (bitarray == (what goes here))
{
{

#6
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
This code
public int StatusToIndex(BitArray status) {

    int[] j = new int[1];

    status.CopyTo(j, 0);

    return j[0];

}

Will convert a 32 'slot' BitArray to an int. This code will convert it back:
public BitArray IndexToStatus(int index) {

    return new BitArray(BitConverter.GetBytes(index));

}

If you need more than one int (32 BitArray 'slots') you'll have to modify the size of the array (and the return type) in the first sample, and allow the passing of an array in the second. Not too hard to do :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users