Check if two BitArray objects are equal in C#

The BitArray class in C# provides a compact way to store and manipulate arrays of bits. However, checking equality between two BitArray objects requires understanding how the Equals()

The BitArray.Equals() method performs reference equality by default, not content comparison. This means it only returns true if both variables point to the same object in memory.

Syntax

Following is the syntax for using the Equals() method −

bool result = bitArray1.Equals(bitArray2);

Reference Equality vs Content Equality

BitArray Equality Comparison Reference Equality arr2 = arr1; arr1.Equals(arr2) returns True Same object reference Content Equality arr1[0] = false, arr1[1] = true arr2[0] = false, arr2[1] = true returns False Different object references

Using Reference Assignment

When you assign one BitArray to another, both variables reference the same object in memory −

using System;
using System.Collections;

public class Demo {
   public static void Main(){
      BitArray arr1 = new BitArray(2);
      BitArray arr2 = new BitArray(2);
      arr1[0] = false;
      arr1[1] = true;
      Console.WriteLine("Elements in BitArray1...");
      foreach (bool res in arr1){
         Console.WriteLine(res);
      }
      arr2[0] = false;
      arr2[1] = true;
      Console.WriteLine("Elements in BitArray2...");
      foreach (bool res in arr2){
         Console.WriteLine(res);
      }
      BitArray arr3 = new BitArray(2);
      arr3 = arr2;
      Console.WriteLine("Is BitArray2 equal to BitArray3? = "+arr2.Equals(arr3));
   }
}

The output of the above code is −

Elements in BitArray1...
False
True
Elements in BitArray2...
False
True
Is BitArray2 equal to BitArray3? = True

Using Content Comparison

When two BitArray objects are created separately, even with identical content, Equals() returns false

using System;
using System.Collections;

public class Demo {
   public static void Main(){
      BitArray arr1 = new BitArray(2);
      BitArray arr2 = new BitArray(2);
      arr1[0] = false;
      arr1[1] = true;
      Console.WriteLine("Elements in BitArray1...");
      foreach (bool res in arr1){
         Console.WriteLine(res);
      }
      arr2[0] = false;
      arr2[1] = true;
      Console.WriteLine("Elements in BitArray2...");
      foreach (bool res in arr2){
         Console.WriteLine(res);
      }
      Console.WriteLine("Is BitArray1 equal to BitArray2? = "+arr1.Equals(arr2));
   }
}

The output of the above code is −

Elements in BitArray1...
False
True
Elements in BitArray2...
False
True
Is BitArray1 equal to BitArray2? = False

Custom Content-Based Equality Method

To perform true content comparison, you need to create a custom method −

using System;
using System.Collections;

public class Demo {
   public static bool AreBitArraysEqual(BitArray arr1, BitArray arr2) {
      if (arr1.Length != arr2.Length) return false;
      for (int i = 0; i 

The output of the above code is −

Reference equality: False
Content equality: True

Conclusion

The BitArray.Equals() method performs reference equality, not content comparison. For content-based equality, you need to implement a custom comparison method that checks each bit individually. Understanding this distinction is crucial when working with BitArray collections in C#.

Updated on: 2026-03-17T07:04:36+05:30

412 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements