Bit Collections

Bit collections are collections whose elements are bit flags. Because each element is a bit instead of an object, these collections behave slightly differently from other collections.

The BitArray class is a collection class in which the capacity is always the same as the count. Elements are added to a BitArray by increasing the Length property; elements are deleted by decreasing the Length property. The BitArray class provides methods that are not found in other collections, including those that allow multiple elements to be modified at once using a filter, such as And, Or, Xor , Not, and SetAll.

The BitVector32 class is a structure that provides the same functionality as BitArray, but with faster performance. BitVector32 is faster because it is a value type and therefore allocated on the stack, whereas BitArray is a reference type and, therefore, allocated on the heap.

BitVector32 can store exactly 32 bits, whereas BitArray can store a variable number of bits. BitVector32 stores both bit flags and small integers, thereby making it ideal for data that is not exposed to the user. However, if the number of required bit flags is unknown, is variable, or is greater than 32, use BitArray instead.

BitArray is in the System.Collections namespace; BitVector32 is in the System.Collections.Specialized namespace.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft