10X Sale
kh logo
All Courses
  1. Tutorials
  2. Programming Tutorials

BitArray Collection in C#

Updated on Sep 3, 2025
 
46,001 Views

The BitArray collection handles a compact matrix of bit values. These bit values are represented as boolean values where true indicates 1 and false indicates 0.

Constructors in BitArray Collection

The different constructors and their description is given as follows:

Constructors

Description

BitArray (BitArray)

This constructor initializes a new instance of the BitArray class that contains the bit values copied from the specified BitArray collection.

BitArray (Boolean [])

This constructor initializes a new instance of the BitArray class that contains the bit values copied from the specified Boolean array.

BitArray (Byte [])

This constructor initializes a new instance of the BitArray class that contains the bit values copied from the specified byte array.

BitArray (Int32)

This constructor initializes a new instance of the BitArray class that can contain the specified number of bit values, initially set to false.

BitArray (Int32, Boolean)

This constructor initializes a new instance of the BitArray class that can contain the specified number of bit values, initially set to the specified value.

BitArray (Int32 [])

This constructor initializes a new instance of the BitArray class that contains the bit values copied from the specified 32-bit integer array.

Table: Constructors in BitArray in C#
Source: MSDN

Properties in BitArray Collection

The different properties and their description is given as follows:

Properties

Description

Count

This property gets the number of elements included in BitArray.

IsReadOnly

This property gets a value that indicates whether BitArray is read-only.

IsSynchronized

This property gets a value that indicates whether access to the BitArray interface is synchronized (it is thread safe).

Item [Int32]

This property gets or sets the value of the bit that is in a specific BitArray position

Length

This property gets or sets the number of BitArray elements.

SyncRoot

This property gets an object that can be used to synchronize access to BitArray.

Table: Properties in BitArray in C#
Source: MSDN

Methods in BitArray Collection

The different methods and their description is given as follows:

Methods

Description

And(Bit Array)

This method performs the AND operation bit by bit between the elements of the current BitArray object and the corresponding elements of the specified array. The current BitArray object will be modified to store the result of the bitwise AND operation

Clone()

This method creates a surface copy of the BitArray collection.

CopyTo( Array, Int32)

This method copies the entire BitArray into a compatible one-dimensional array, starting at the specified index of the target array.

Equals(Object)

This method determines whether the specified object is equal to the current object.

Get(Int32)

This method gets the value of the bit in a specific BitArray position

GetEnumerator()

This method returns an enumerator that iterates through the BitArray collection.

GetHashCode()

This method serves as the default hash function.

GetType()

This method gets the Type of the current instance.

MemberwiseClone()

This method creates a surface copy of the current Object.

Not()

This method inverts all the bit values that are in the current BitArray collection, so that the elements set in true change to false and the elements set in false change toa true.

Or(Bit Array)

This method performs the bitwise OR operation between the elements of the current BitArrayobject and the corresponding elements of the specified array. The current BitArray object will be modified to store the result of the bitwise OR operation.

Set(Int32, Boolean)

This method sets the bit located at a specific BitArray position to the specified value

SetAll (Boolean)

This method sets all BitArray bits to the specified value.

ToString()

This method returns a string that represents the current object.

Xor(Bit Array)

This method performs the exclusive OR bitwise operation between the elements of the current BitArray object against the corresponding elements of the specified array. The current BitArray object will be modified to store the result of the bitwise exclusive OR operation.

Table: Methods in BitArray in C#
Source: MSDN

BitArray Operations

Some of the BitArray operations are given as follows:

Create a BitArray

A BitArray can be created using new. Then the appropriate boolean values can be inserted into a BitArray. The program that demonstrates this is given as follows:

using System;
using System.Collections;
namespace BitArrayDemo
{
  class Example
  {
     static void Main(string[] args)
     {
       BitArray bArr = new BitArray(5);
       bArr.Set(0, true);
       bArr.Set(1, false);
       bArr.Set(2, true);
       bArr.Set(3, true);
       bArr.Set(4, false);
       for (int i = 0; i < bArr.Count; i++)
       {
           Console.Write(bArr [i]);
           Console.Write(" ");
       }
     }
  }
}

Source Code: Program to create BitArray in C#

The output of the above program is as follows:

True False True True False

AND Operation in BitArray

The And() method performs the AND operation bit by bit between the elements of the current BitArray and the corresponding elements of the BitArray that is specified. The program that demonstrates this is given as follows:

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

Source Code: Program to demonstrate AND operation in BitArray in C#

The output of the above program is as follows:

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

OR Operation in BitArray

The Or() method performs the OR operation bit by bit between the elements of the current BitArray and the corresponding elements of the BitArray that is specified. The program that demonstrates this is given as follows:

using System;
using System.Collections;
namespace BitArrayDemo
{
  class Example
  {
     static void Main(string[] args)
     {
       BitArray barr1 = new BitArray(5);
       BitArray barr2 = new BitArray(5);
       barr1.Set(0, true);
       barr1.Set(1, false);
       barr1.Set(2, true);
       barr1.Set(3, true);
       barr1.Set(4, false);
       barr2.Set(0, true);
       barr2.Set(1, true);
       barr2.Set(2, false);
       barr2.Set(3, true);
       barr2.Set(4, false);
       Console.WriteLine("BitArray 1");
       for (int i = 0; i < barr1.Count; i++)
       {
           Console.Write(barr1[i]);
           Console.Write(" ");
       }
       Console.WriteLine();
       Console.WriteLine("BitArray 2");
       for (int i = 0; i < barr2.Count; i++)
       {
           Console.Write(barr2[i]);
           Console.Write(" ");
       }
       barr1.Or(barr2);
       Console.WriteLine();
       Console.WriteLine("BitArray after OR operation");
       for (int i = 0; i < barr1.Count; i++)
       {
           Console.Write(barr1[i]);
           Console.Write(" ");   
       }  
     }
  }
}

Source Code: Program to demonstrate OR operation in BitArray in C#

The output of the above program is as follows:

BitArray 1
True False True True False
BitArray 2
True True False True False
BitArray after OR operation
True True True True False

NOT Operation in BitArray

The Not() method inverts all the elements in the specified BitArray so the true elements are converted to false and the false elements are converted to true. The program that demonstrates this is given as follows:

using System;
using System.Collections;
namespace BitArrayDemo
{
  class Example
  {
     static void Main(string[] args)
     {
       BitArray barr = new BitArray(5);
       barr.Set(0, true);
       barr.Set(1, false);
       barr.Set(2, true);
       barr.Set(3, true);
       barr.Set(4, false);
       Console.WriteLine("BitArray");
       for (int i = 0; i < barr.Count; i++)
       {
           Console.Write(barr[i]);
           Console.Write(" ");            
       }
       barr.Not();
       Console.WriteLine();
       Console.WriteLine("BitArray after NOT operation");
       for (int i = 0; i < barr.Count; i++)
       {
           Console.Write(barr[i]);
           Console.Write(" ");
       }
     }
  }
}

Source Code: Program to demonstrate NOT operation in BitArray in C#

The output of the above program is as follows:

BitArray
True False True True False
BitArray after NOT operation
False True False False True

Check whether the BitArray is ReadOnly or not

Use the IsReadOnly property to check whether the BitArray is ReadOnly or not. If a collection is ReadOnly, then you won’t be able to add new elements to it.

Let us see an example that checks whether the BitArray is ReadOnly or not:

using System;
using System.Collections;
namespace BitArrayDemo
{
  class Example
  {
     static void Main(string[] args)
     {
       BitArray bArr = new BitArray(5);
       bArr.Set(0, true);
       bArr.Set(1, false);
       bArr.Set(2, true);
       bArr.Set(3, true);
       for (int i = 0; i < bArr.Count; i++)
       {
           Console.Write(bArr [i]);
           Console.Write(" ");
       }
       Console.WriteLine("\nIsReadOnly? " + bArr.IsReadOnly);
     }
  }
}

Source Code: Program to check whether the BitArray is ReadOnly or not in C#

The above program gives the following output:

True False True True False
IsReadOnly? False

Get the length of the elements in a BitArray

To get the length of the BitArray elements, use the Length property. Let us see an example:

using System;
using System.Collections;
public class Example  {
  public static void Main()  {
     BitArray bArr1 = new BitArray(15);
     BitArray bArr2 = new BitArray(5);
       bArr2.Set(0, true);
       bArr2.Set(1, false);
       bArr2.Set(2, true);
       bArr2.Set(3, true);
       bArr2.Set(4, false);
       for (int i = 0; i < bArr2.Count; i++)
       {
           Console.Write(bArr2 [i]);
           Console.Write(" ");
       }
     Console.WriteLine( "\nLength: {0}", bArr1.Length );
     Console.WriteLine( "Length: {0}", bArr2.Length );
  }
}

Source Code: Program to get the length of the BitArray elements in C#

The above program gives the following output:

True False True True False
Length: 15
Length: 5

Get the count of the elements in a BitArray

To get the count of the elements in BitArray, use the Count property. Let us see an example:

using System;
using System.Collections;
public class Demo  {
  public static void Main()  {
     BitArray bArr1 = new BitArray( 10 );
     BitArray bArr2 = new BitArray(5);
       bArr2.Set(0, true);
       bArr2.Set(1, false);
       bArr2.Set(2, true);
       bArr2.Set(3, true);
       bArr2.Set(4, false);
       for (int i = 0; i < bArr2.Count; i++)
       {
           Console.Write(bArr2 [i]);
           Console.Write(" ");
       }
     Console.WriteLine( "\nCount: {0}", bArr1.Count );
     Console.WriteLine( "Count: {0}", bArr2.Count );
  }
}

Source Code: Program to get the count of the elements in BitArray in C#

The above program gives the following output:

True False True True False
Count: 10
Count: 5

Fetch a value from a BitArray Collection

In a BitArray collection, you may need to fetch a value. For that, the following is the code. Here, we are displaying a value from the BitArray elements:

using System;
using System.Collections;
public class Example  {
  public static void Main()  {
     BitArray bArr1 = new BitArray(15);
     BitArray bArr2 = new BitArray(5);
       bArr2.Set(0, true);
       bArr2.Set(1, false);
       bArr2.Set(2, true);
       bArr2.Set(3, true);
       bArr2.Set(4, false);
       for (int i = 0; i < bArr2.Count; i++)
       {
           Console.Write(bArr2 [i]);
           Console.Write(" ");
       }
     Console.WriteLine( "\nLength: {0}", bArr1.Length );
     Console.WriteLine( "Length: {0}", bArr2.Length );
     bool s = bArr2[3];
     Console.WriteLine("4th element ="+s);
  }
}

Source Code: Program to fetch a value from a BitArray collection in C#

The above program generated the following output:

True False True True False
Length: 15
Length: 5
4th element =True
+91

By Signing up, you agree to ourTerms & Conditionsand ourPrivacy and Policy

Get your free handbook for CSM!!
Recommended Courses