C# Reference ยท Collections
The BitArray class
Reference for C# BitArray: a compact array of bool values, Get, Set, SetAll, And, Or, Xor, Not, shifts, copying, and when to prefer bool[] or a bit mask.
BitArray (namespace System.Collections) stores a fixed number of bits, packed 32 per int, and exposes them as bool values. It is useful when you need millions of flags in little memory, or when you want to combine two sets of flags with AND, OR, XOR and NOT in one call.
BitArray is a non-generic collection, so enumerating it gives object (boxed bool). Index it instead, or cast with Cast<bool>(). Its length is set at construction and can be changed through Length, but you cannot add or remove single bits.
var bits = new BitArray(8); bits[2] = true; bits.Set(5, true); bool on = bits.Get(2); // true
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public sealed class BitArray : ICollection, IEnumerable, ICloneable { public BitArray(int length); public BitArray(int length, bool defaultValue); public BitArray(bool[] values); public BitArray(byte[] bytes); public BitArray(int[] values); public BitArray(BitArray bits); public int Length { get; set; } public int Count { get; } public bool this[int index] { get; set; } public bool Get(int index); public void Set(int index, bool value); public void SetAll(bool value); public BitArray And(BitArray value); public BitArray Or(BitArray value); public BitArray Xor(BitArray value); public BitArray Not(); public BitArray LeftShift(int count); public BitArray RightShift(int count); public void CopyTo(Array array, int index); }
| Name | Description |
|---|---|
length |
Number of bits. |
defaultValue |
Initial value of every bit (default false). |
values / bytes |
Initial bits. For byte[] and int[], the least significant bit of element 0 becomes bit 0, so Length is 8 or 32 times the array length. |
index |
Zero-based bit position. |
value (And/Or/Xor) |
The other array. Both must have the same Length. |
Returns: And, Or, Xor, Not and the shifts change the array in place and return this, which allows chaining.
Errors: ArgumentOutOfRangeException for a bad index or negative length, ArgumentException when And, Or or Xor is called with an array of a different length, ArgumentNullException for null.
Usage notes
- The operators mutate.
a.And(b)changesa. Copy first withnew BitArray(a)when you still need the original, as in the second example. - Shifts keep the length.
LeftShift(n)moves bits toward higher indexes and fills withfalse; bits shifted past the end are lost. - Enumeration boxes.
foreach (bool bit in bits)works because the compiler casts eachobject, but it allocates. Aforloop over the indexer is faster. - Counting bits. There is no
PopCountmethod. Loop, or copy toint[]and useSystem.Numerics.BitOperations.PopCounton each element. - When not to use it. For 64 flags or fewer, an
enumwith[Flags]or aulongmask is simpler and faster. For sparse data use aHashSet<int>.bool[]is quicker to read and write thanBitArraywhen memory is not a concern. - Memory: 1 bit per flag plus a small object header, versus 1 byte per
boolin an array. - Not thread-safe and not generic.