Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitarray VS bool[]

I expected to find a existing question here on SO about this but i didn't.

What is the advantage of using a Bitarray when you can store your bool values in a bool[]?

System.Collections.BitArray biArray = new System.Collections.BitArray(8);
biArray[4] = true;

bool[] boArray = new bool[8];
boArray[4] = true;

The bool[] seems a little more handy to me because there exist more (extension)methods to work with a array instead of a BitArray

like image 557
Byyo Avatar asked Nov 03 '15 13:11

Byyo


People also ask

What is the difference between a bool and a bit array?

A bool array will store each entry as one byte and thus taking up more memory, but requiring fewer CPU cycles to access. Essentially, BitArray is a memory optimization over bool[], but there's no point in using it unless memory is sparse. Edit: Created a simple performance test. Inversing 500M elements using BitArray takes 6 seconds on my machine:

What is BitArray in Java?

BitArray is compact and allows you to perform bitwise operations. From the MSDN forum : A BitArray uses one bit for each value, while a bool[] uses one byte for each value. You can pass to the BitArray constructor either an array of bools, an array of bytes or an array of integers.

What is a booleans array in system collections?

System. Collections System. Collections Manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on (1) and false indicates the bit is off (0). The following code example shows how to create and initialize a BitArray and how to print out its values.

What are the key features of Python BitArray?

Key Features of Python Bitarray: The object of the bit array has a similar behavior as that of the list. So, the operations performed on the list like slicing can be performed on bit array object as well It is possible to pack or unpack bit array into other data formats like ndarray


2 Answers

There's a memory/performance tradeoff. BitArray will store 8 entries per byte, but accessing a single entry requires a bunch of logical operations under the hood. A bool array will store each entry as one byte and thus taking up more memory, but requiring fewer CPU cycles to access.

Essentially, BitArray is a memory optimization over bool[], but there's no point in using it unless memory is sparse.

Edit: Created a simple performance test. Inversing 500M elements using BitArray takes 6 seconds on my machine:

const int limit = 500000000;
var bitarray = new BitArray(limit);
for (var i = 0; i < limit; ++i)
{
    bitarray[i] = !bitarray[i];
}

The same test using a bool array takes about 1.5 seconds:

const int limit = 500000000;
var boolarray = new bool[limit];
for (var i = 0; i < limit; ++i)
{
    boolarray[i] = !boolarray[i];
}
like image 141
Kvam Avatar answered Oct 16 '22 18:10

Kvam


BitArray is compact and allows you to perform bitwise operations. From the MSDN forum :

A BitArray uses one bit for each value, while a bool[] uses one byte for each value. You can pass to the BitArray constructor either an array of bools, an array of bytes or an array of integers. You can also pass an integer value specifying the desired length and (optionally) a boolean argument that specifies if the individual bits should be set or not.

like image 30
Rahul Tripathi Avatar answered Oct 16 '22 20:10

Rahul Tripathi