3

I'm in trouble with a BitArray.

The goal is to simulate a stack of 8 80bit BitArrays, numbered from 0 to 7.

I just need to be able to access them by index, and so I think a simple array will be enough for me.

When initialising a BitArray object, I need to specify the number of bits it will contain, which gives me

BitArray test = new BitArray(80);

How can I do an Array of it, knowing I need to specify the length value?

I've tried several things, like

BitArray[] stack = new BitArray(80)[];

but I always get an error when trying to give it the length...

Any thoughts?

Thanks in advance

3 Answers 3

4

Unfortunately, the framework doesn't appear to have a "canonical" array-initialization pattern, as far as I know.

One way, using LINQ, would be:

var stack = Enumerable.Range(0, 8)
                      .Select(i => new BitArray(80))
                      .ToArray();

or:

var stack = Enumerable.Repeat<Func<BitArray>>( () => new BitArray(80), 8)
                      .Select(f => f())
                      .ToArray();

Alternatively,

BitArray[] stack = new BitArray[8];

for(int i = 0; i < stack.Length; i++)
   stack[i] = new BitArray(80);
Sign up to request clarification or add additional context in comments.

Comments

2

First create your BitArray array ([]) like this:

BitArray[] stack = new BitArray[8];

and then initialize all seperate bitarrays in a for-loop (something like this):

foreach (BitArray arr in stack)
{
    arr = new BitArray(80);
}

Edit: the something like this was more or less a pointer, not actually tested; this below is:

BitArray[] stack = new BitArray[8];
for(int i=0;i<stack.Length;i++)
{
    stack[i] = new BitArray(80);
}
stack[0][0] = true;

5 Comments

This won't work. You'd get NullReferenceException for each instance arr. You need to set individual array members with a for loop and a code like stack[n] = new BitArray(80);
It doesn't make sense to foreach the array here, unfortunately.
I did say "something like this".. didn't test it; just pointed to a solution. Topicstarter wants to combine the two - which wont work.
Hmm? I wouldn't expect an exception here, it just wouldn't do anything. The effect is, each time through the loop, 'arr' is made to refer to one of the array elements, and then made to refer to the new BitArray (without affecting the array).
I don't it think it will compile, since the loop variable in a foreach is read-only, isn't it?
-1

Well...

I finally did it this way:

List<BitArray> stack = new List<BitArray>(8);

public FPU()
{
    //initialise the stack

    for (int i = 0; i < stack.Capacity; i++)
    {
        stack[i] = new BitArray(80);
    }
}

Thanks for your answers, which leaded me to this solution, wich seems to work for me.

Have a nice day, and again, thanks!

2 Comments

Better use Count instead of Capacity.
Yes, noticed that while testing. I've replaced the stack count by a constant, for flexibility.

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.