BitArray

The BitArray class supports a collection of bits. Because it stores bits rather than objects, BitArray has capabilities different from those of the other collections. However, it still supports the basic collection underpinning by implementing ICollection and IEnumerable. It also implements ICloneable.
 
BitArray defines several constructors. You can construct a BitArray from an array of Boolean values using this constructor:
public BitArray(bool[ ] values)
In this case, each element of values becomes a bit in the collection. Thus, each bit in the collection corresponds to an element of values. Furthermore, the ordering of the elements of values and the bits in the collection are the same. You can create a BitArray from an array of bytes using this constructor:
 
public BitArray(byte[ ] bytes)
Here, the bit pattern in bytes becomes the bits in the collection, with bytes[0] specifying the first 8 bits, bytes[1] specifying the second 8 bits, and so on. In similar fashion, you can construct a BitArray from an array of ints using this constructor:
 
public BitArray(int[ ] values)
In this case, values[0] specifies the first 32 bits, values[1] specifies the second 32 bits, and so on. You can create a BitArray of a specific size using this constructor:
 
public BitArray(int length)
Here, length specifies the number of bits. The bits in the collection are initialized to false. To specify a size and initial value of the bits, use the following constructor:
 
public BitArray(int length, bool defaultValue)
In this case, all bits in the collection will be set to the value passed in defaultValue. Finally, you can create a new BitArray from an existing one by using this constructor:
 
public BitArray(BitArray bits)
The new object will contain the same collection of bits as bits, but the two collections will be otherwise separate.
 
BitArrays can be indexed. Each index specifies an individual bit, with an index of zero indicating the low-order bit.
 
In addition to the methods specified by the interfaces that it implements, BitArray defines the methods shown in Following Table. Notice that BitArray does not supply a Synchronized( ) method. Thus, a synchronized wrapper is not available, and the IsSynchronized property is always false. However, you can control access to a BitArray by synchronizing on the object provided by SyncRoot.

Method Description
public BitArray And(BitArray value) ANDs the bits of the invoking object with those specified by value and returns a BitArray that contains the result.
public bool Get(int index) Returns the value of the bit at the index specified by index.
public BitArray Not( ) Performs a bitwise, logical NOT on the invoking collection and returns a BitArray that contains the result.
public BitArray Or(BitArray value) ORs the bits of the invoking object with those specified by value and returns a BitArray that contains the result.
public void Set(int index, bool value) Sets the bit at the index specified by index to value.
public void SetAll(bool value) Sets all bits to value.
public BitArray Xor(BitArray value) XORs the bits of the invoking object with those specified by value and returns a BitArray that contains the result.

Example

using System;
using System.IO;
using System.Collections;
namespace ConsoleApplication1
{
    class Program
    {
        public static void ShowBits(string rem,
        BitArray bits)
        {
            Console.WriteLine(rem);
            for (int i = 0; i < bits.Count; i++)
                Console.Write("{0, -6} ", bits[i]);
            Console.WriteLine("\n");
        }
        static void Main()
        {
            BitArray ba = new BitArray(8);
            byte[] b = { 67 };
            BitArray ba2 = new BitArray(b);
            ShowBits("Original contents of ba:", ba);
            ba = ba.Not();
            ShowBits("Contents of ba after Not:", ba);
            ShowBits("Contents of ba2:", ba2);
            BitArray ba3 = ba.Xor(ba2);
            ShowBits("Result of ba XOR ba2:", ba3);
            Console.ReadKey();
        }
    }
}