Topic : C# Non-Generic Collection - BitArray Class in C# Programming Language:

BitArray:

The BitArray Class Manages a Compact array of Bit Values, which are Represented as Booleans, Where True indicates that bit is on (1) and False Indicates the bit is Off (0).

We use BitArray class when we need to store the bits but do not know the number of bits in advance. we can access items from the BitArray collection by using an Integer index, which starts from zero.




BitArray : infobrother

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, and elements are deleted by decreasing the Length Property. The size of a BitArray is controlled by the client. indexing past the end of BitArray Throws an ArgumentException. The BitArray Class provides some useful methods and properties to work with it. The following table lists some of the commonly used Properties of the BitArray Class.



PropertiesDescription
CountGets the number of elements contained in the BitArray.
IsReadOnlyGets a value indicating whether the BitArray is read-only.
ItemGets or sets the value of the bit at a specific position in the BitArray.
LengthGets or sets the number of elements in the BitArray.


The following table lists some of the commonly used Methods of the BitArray Class.

MethodsDescription
And(BitArray)Performs the bitwise AND operation between the elements of the current BitArray object and the corresponding elements in the specified array. The current BitArray object will be modified to store the result of the bitwise AND operation.
Equals()Determines whether the specified object is equal to the current object.
Get()Gets the value of the bit at a specific position in the BitArray.
GetType()Gets the Type of the current instance.
Not()Inverts all the bit values in the current BitArray, so that elements set to true are changed to false, and elements set to false are changed to true.
Or(BitArray)Performs the bitwise OR operation between the elements of the current BitArray object and the corresponding elements in the specified array. The current BitArray object will be modified to store the result of the bitwise OR operation.
Set(int, Boolean)Sets the bit at a specific position in the BitArray to the specified value.
SetAll(Boolean)Sets all bits in the BitArray to the specified value.
Xor(BitArray)Performs the bitwise exclusive OR operation between the elements of the current BitArray object against the corresponding elements in the specified array. The current BitArray object will be modified to store the result of the bitwise exclusive OR operation.
For complete list of properties and methods, please visit Microsoft's C# documentation.


The following example demonstrates the use of BitArray Class. In this example we will try to use most of the common methods and properties of the BitArray Class to clear the concept about it.



/*Example - BitArray Class - InfoBrother*/

using System;
using System.Collections;
public class MyBitArray
{

    public static void Main()
    {
        // Creates and initializes several BitArrays.
        BitArray Bit1 = new BitArray(5);

        BitArray Bit2 = new BitArray(5, false);

        byte[] myBytes = new byte[5] { 1, 2, 3, 4, 5 };
        BitArray Bit3 = new BitArray(myBytes);

        bool[] myBools = new bool[5] { true, false, true, true, false };
        BitArray Bit4 = new BitArray(myBools);

        int[] myInts = new int[5] { 6, 7, 8, 9, 10 };
        BitArray Bit5 = new BitArray(myInts);


        // Displays the properties and values of the BitArrays.
        Console.WriteLine("Bit # 1: (Bit1)");
        Console.WriteLine("   Count:    {0}", Bit1.Count);
        Console.WriteLine("   Length:   {0}", Bit1.Length);
        Console.WriteLine("   Values:");
        PrintValues(Bit1, 8);

        Console.WriteLine("\nBit # 2: (Bit2)");
        Console.WriteLine("   Count:    {0}", Bit2.Count);
        Console.WriteLine("   Length:   {0}", Bit2.Length);
        Console.WriteLine("   Values:");
        PrintValues(Bit2, 8);

        Console.WriteLine("\nBit # 3: (Bit3)");
        Console.WriteLine("   Count:    {0}", Bit3.Count);
        Console.WriteLine("   Length:   {0}", Bit3.Length);
        Console.WriteLine("   Values:");
        PrintValues(Bit3, 8);

        Console.WriteLine("\nBit # 4: (Bit4)");
        Console.WriteLine("   Count:    {0}", Bit4.Count);
        Console.WriteLine("   Length:   {0}", Bit4.Length);
        Console.WriteLine("   Values:");
        PrintValues(Bit4, 8);

        Console.WriteLine("\nBit # 5: (Bit5)");
        Console.WriteLine("   Count:    {0}", Bit5.Count);
        Console.WriteLine("   Length:   {0}", Bit5.Length);
        Console.WriteLine("   Values:");
        PrintValues(Bit5, 8);

        Console.ReadKey();
    }


    public static void PrintValues(IEnumerable myList, int myWidth)
    {
        int i = myWidth;
        foreach (Object obj in myList)
        {
            if (i <= 0)
            {
                i = myWidth;
                Console.WriteLine();
            }
            i--;
            Console.Write("{0,8}", obj);
        }
        Console.WriteLine();
    }

}


For complete list of properties and methods, please visit Microsoft's C# documentation.
















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement