In this article I will be Explaining the use of BitArray Collection
The BitArray Collection Type
BitArray :- The BitArray collections can only store Boolean Values and cannot dynamically increase its size so in the constructor of the BitArray we need to specify the Capacity.The size can be increased by using the Length property.The BitArray Class is in the
System.Collections namespace.
Example
BitArray com = new BitArray(5);
com[0] = true;
com[1] = false;
com[2] = true;
In the Above code The Bit Array is created with the size of 5 and only 3 have been initialised the rest 2 will be false by default.
To display the output
foreach (bool item in com)
{
Console.WriteLine(item);
}
The output will be
True
False
True
False
False
Apart from only storing Boolean values in the BitArray class it also supports common Bit-level operations such as OR,AND ,Xor(eXclusive OR) and Not.
Example : To demonstrate the use of BitLevel operations
OR
BitArray com = new BitArray(4);
com[0] = true;
com[1] = false;
com[2] = true;
com[3] = true;
BitArray com1 = new BitArray(4);
com1[0] = false;
com1[1] = true;
com1[2] = false;
com1[3] = true;
BitArray orComCom1 = com.Or(com1);
Console.WriteLine(" COM\tCOM1\tOR");
Console.WriteLine("------------------------------------");
for (int i = 0; i < com.Length; i++)
{
Console.WriteLine("{0}\t{1}\t{2}",com[i],com1[i],orComCom1[i]);
}
Similarly you can try it with the Other Bit level Operators.
P.S.: The BitArray Class does not have the Add() or Remove() method because each value in the BitArray can either be True or False.
Regards
Hefin Dsouza