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
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:
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.
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.
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
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];
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With