1

I'm attempting to use a multidimensional BitArray but I'm stuck with how to set or read bits with it.

With a normal one dimension BitArray I can simply do the following to set a bit:

bitArray.Set(0, true);

However I don't know how to do the same with a two-dimension bit array. For example the following code does not make sense, as the Set method requires an index but I've already supplied the index previously in the "[0, 0]":

 bitArray[0, 0].Set(0, true);

My question: What's the proper way of making and then using a multidimensional BitArray?

asked May 18, 2014 at 2:04
2
  • You don't need a 2-dimensional array of BitArray instances as BitArray already contains a 1-dimensional array. Just use BitArray[] for 2-dimensional storage. Commented May 18, 2014 at 2:07
  • Can you elaborate on that? Commented May 18, 2014 at 2:10

2 Answers 2

8

An instance of BitArray is not an array as far the CLR is concerned (that is, BitArray is not an "array type"). If you want to store 2-dimensional bit information you have a few options (all of my examples create a 10x20 2D volume):

a) Use a single array of BitArray like so:

// Init:
BitArray[] storage = new BitArray[ 20 ];
for(int y=0;y<storage.Length;y++) storage[y] = new BitArray( 10, true );
// Usage:
Boolean at5x7 = storage[7][5];

b) Use the BitArray as a 2D space in itself, by indexing by row-and-column (this will actually be faster as the CLR won't invoke its Bounds-checking as often):

// Init:
const Int32 width = 10, height = 20;
BitArray storage = new BitArray( width * height );
// Usage:
Boolean at5x7 = storage[ (5 * width) + 7];
answered May 18, 2014 at 2:11
Sign up to request clarification or add additional context in comments.

1 Comment

To note that the 2D bool array and 2D indexed BitArray are in no way equivalent. The bool array will be much larger as booleans take entire bytes while BitArray applies bitwise operations to condense each value down to one bit.
1
public sealed class BitArray2D
{
 private BitArray _array;
 private int _dimension1;
 private int _dimension2;
 public BitArray2D(int dimension1, int dimension2)
 {
 _dimension1 = dimension1 > 0 ? dimension1 : throw new ArgumentOutOfRangeException(nameof(dimension1), dimension1, string.Empty);
 _dimension2 = dimension2 > 0 ? dimension2 : throw new ArgumentOutOfRangeException(nameof(dimension2), dimension2, string.Empty);
 _array = new BitArray(dimension1 * dimension2);
 }
 public bool Get(int x, int y) { CheckBounds(x, y); return _array[y * _dimension1 + x]; }
 public bool Set(int x, int y, bool val) { CheckBounds(x, y); return _array[y * _dimension1 + x] = val; }
 public bool this[int x, int y] { get { return Get(x, y); } set { Set(x, y, value); } }
 private void CheckBounds(int x, int y)
 {
 if (x < 0 || x >= _dimension1)
 {
 throw new IndexOutOfRangeException();
 }
 if (y < 0 || y >= _dimension2)
 {
 throw new IndexOutOfRangeException();
 }
 }
}
answered Sep 28, 2019 at 21:20

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.