Working with System.Collections.BitArray in C#

In the last post we have seen how to work with and ArrayList using C#. In this article I will explore the class BitArray and the methods provided.

BitArray class is designed to hold the values as bits. These values represent, either true or false. It is the simplest of collections and provides Bitwise operations like XOR, AND etc. The class is sealed and cannot be inherited and inherits from intefaces as follows,

public sealed class BitArray : ICollection, 
    IEnumerable, ICloneable

class provides following constructors

           // Declare BitArray
            BitArray bitArray;

            // BitArray with 5 objects
            bitArray = new BitArray(5);  

            // constructor which takes a byte Array
            bitArray = new BitArray(new byte[3]);

            // constructor which takes a int Array          
            bitArray = new BitArray(new int[5] { 1, 2, 4, 5, 7 });

            // constructor which takes a number of 
            // bejts and default value to initialize with
            bitArray = new BitArray(5, true);

            // constructor which takes a bool Array
            bitArray = new BitArray(new bool[] { true, true, true, true, false });

Here are the remaining methods with usage sample

            // perform the And operation for this BitArray 
            // with another BitArray a return a new BitArray
            bitArray = bitArray.And(bitArray);

            // use cast to convert BitArray
            // to an Array of Type T
            string[] arrResult = bitArray.Cast<string>().ToArray<string>();

            // Copy item of a BitArray to another Array
            bitArray.CopyTo(new BitArray[5], 0);

            // count total items in a BitArray 
            int total = bitArray.Count;

            // compare BitArray with another Object
            bool IsEqualsTo = bitArray.Equals(new BitArray[5]);

            // get some value from  location
            bool somValue = bitArray.Get(0);

            // get an Enummerator 
            e = bitArray.GetEnumerator();
            while (e.MoveNext())
                Object obj = e.Current;

            // get Type of BitArray 
            Type type = bitArray.GetType();

            // check if is BitArray is read only
            bool IsReadOnly = bitArray.IsReadOnly;

            //  get the length of BitArray
            int length = bitArray.Length;

            //invert all teh items in BitArray
            bitArray = bitArray.Not();

            // perform Or operation with another BitArray
            // and return a new BitArray
            bitArray = bitArray.Or(bitArray);

            // set value at location
            bitArray.Set(0, true);

            // perform Xor operation with another BitArray
            // and return a new BitArray
            bitArray = bitArray.Xor(bitArray);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s