views:

1507

answers:

2

Hi all,

What is the difference between BitArray and BitVector 32 structure and what are the advantages of BitVector 32 structure over BitArray? Why is the BitVector 32 structure more efficient than BitArray?

Thanks in advance.

Jay...

+2  A: 

I had an answer based on my intuition but I was not sure because I do not know C# yet. Anyway, to verify my instinctive reaction, I searched Google for BitVector32. The first was Microsoft's documentation:

http://msdn.microsoft.com/en-us/library/system.collections.specialized.bitvector32.aspx

BitVector32 is more efficient than BitArray for Boolean values and small integers that are used internally. A BitArray can grow indefinitely as needed, but it has the memory and performance overhead that a class instance requires. In contrast, a BitVector32 uses only 32 bits.

Sinan Ünür
But in BitVector32 also we create an intance of that type. BitVector32 newVector = new BitVector32(4); //like this.
Jaywith.7
@Jaywith.7 » A new instance of a struct doesn't have the same overhead as an instance of a class.
John Feminella
The capacity of BitVector32 is limited to 32 bits, the size of an int. Therefore, indexing and masking can be single ops. Compare this to a bit array with 734 bits and you want to find out if bit 197 is set. Think about how you would do that (from the perspective of the class designer). Remember, a bit array is not an array of bools internally.
Sinan Ünür
+1  A: 

BitVector32 is more efficient than BitArray for Boolean values and small integers that are used internally. A BitArray can grow indefinitely as needed, but it has the memory and performance overhead that a class instance requires. In contrast, a BitVector32 uses only 32 bits.

http://msdn.microsoft.com/en-us/library/system.collections.specialized.bitvector32.aspx

BitVector32 is a struct and consumes only 4 bytes. BitArray is a class that has overheads associated with it and is therefore less efficient - BitArray will need at least 8 bytes before you've even added any objects to it as it lives on the heap. More about the stack and heap here.

Matthew Brindley