1.

What is a BitArray class in C#?

Answer»

The BitArray class in C# manages a COMPACT array of BIT VALUES which are represented as boolean values where true indicates 1 (the bit is on) and false indicates 0(the bit is off).

The BitArray class is usually used if the number of bits that need to be used are not known in advance but bits are required. The bits are indexed using an integer index that STARTS from 0 and can be accessed using this index.

A program that demonstrates the BitArray class in C# is given as follows:

using System; using System.Collections; namespace BitArrayDemo {   class Example   {      static void Main(string[] args)      {        BitArray ba1 = new BitArray(5);        BitArray BA2 = new BitArray(5);        ba1.Set(0, true);        ba1.Set(1, true);        ba1.Set(2, false);        ba1.Set(3, true);        ba1.Set(4, false);        ba2.Set(0, false);        ba2.Set(1, true);        ba2.Set(2, false);        ba2.Set(3, true);        ba2.Set(4, false);        Console.Write("BitArray 1 is: ");        for (int i = 0; i < ba1.Count; i++)        {            Console.Write(ba1[i] + " ");        }        Console.Write("\nBitArray 2 is: ");        for (int i = 0; i < ba2.Count; i++)        {            Console.Write(ba2[i] + " ");        }        ba1.And(ba2);        Console.Write("\nBitArray after AND operation is: ");        for (int i = 0; i < ba1.Count; i++)        {            Console.Write(ba1[i] + " ");        }      }   } }

The output of the above program is as follows:

BitArray 1 is: True True False True False BitArray 2 is: False True False True False BitArray after AND operation is: False True False True False


Discussion

No Comment Found