What is index-based I/O BitArray collection in C#?

The BitArray class in C# manages a compact array of bit values represented as Boolean values, where true indicates the bit is on (1) and false indicates the bit is off (0). It is part of the System.Collections namespace and provides an efficient way to store and manipulate bits.

BitArray is particularly useful for scenarios requiring bitwise operations, boolean flags, or when memory efficiency is crucial since it stores bits compactly rather than using full bytes for each boolean value.

Syntax

Following is the syntax for creating a BitArray −

BitArray bitArray = new BitArray(size);
BitArray bitArray = new BitArray(bool[] values);
BitArray bitArray = new BitArray(byte[] bytes);

BitArray Methods

The following table shows the key methods available in the BitArray class −

Sr.No. Method & Description
1 public BitArray And(BitArray value);
Performs the bitwise AND operation on the elements in the current BitArray against the corresponding elements in the specified BitArray.
2 public bool Get(int index);
Gets the value of the bit at a specific position in the BitArray.
3 public BitArray 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.
4 public BitArray Or(BitArray value);
Performs the bitwise OR operation on the elements in the current BitArray against the corresponding elements in the specified BitArray.
5 public void Set(int index, bool value);
Sets the bit at a specific position in the BitArray to the specified value.
6 public void SetAll(bool value);
Sets all bits in the BitArray to the specified value.
7 public BitArray Xor(BitArray value);
Performs the bitwise eXclusive OR operation on the elements in the current BitArray against the corresponding elements in the specified BitArray.

BitArray Operations Array A: 1 0 1 1 Array B: 0 1 1 0 A.And(B): 0 0 1 0 A.Or(B): 1 1 1 1 A.Xor(B): 1 1 0 1 Blue = 1 (true) Orange = 0 (false)

Using BitArray Creation and Basic Operations

Example

using System;
using System.Collections;

public class Demo {
   public static void Main() {
      BitArray arr = new BitArray(10);
      Console.WriteLine("Count: {0}", arr.Count);
      Console.WriteLine("Length: {0}", arr.Length);
      
      // Set some bits
      arr.Set(0, true);
      arr.Set(2, true);
      arr.Set(5, true);
      
      Console.Write("BitArray values: ");
      for (int i = 0; i < arr.Count; i++) {
         Console.Write(arr.Get(i) ? "1 " : "0 ");
      }
      Console.WriteLine();
   }
}

The output of the above code is −

Count: 10
Length: 10
BitArray values: 1 0 1 0 0 1 0 0 0 0 

Using BitArray Bitwise Operations

Example

using System;
using System.Collections;

public class Demo {
   public static void Main() {
      bool[] vals1 = { true, false, true, true };
      bool[] vals2 = { false, true, true, false };
      
      BitArray ba1 = new BitArray(vals1);
      BitArray ba2 = new BitArray(vals2);
      
      Console.Write("BitArray 1: ");
      PrintBitArray(ba1);
      
      Console.Write("BitArray 2: ");
      PrintBitArray(ba2);
      
      // AND operation
      BitArray ba3 = ba1.And(ba2);
      Console.Write("After AND: ");
      PrintBitArray(ba1); // ba1 is modified
      
      // Reset ba1 and perform OR
      ba1 = new BitArray(vals1);
      ba1.Or(ba2);
      Console.Write("After OR:  ");
      PrintBitArray(ba1);
   }
   
   static void PrintBitArray(BitArray ba) {
      for (int i = 0; i < ba.Count; i++) {
         Console.Write(ba[i] ? "1 " : "0 ");
      }
      Console.WriteLine();
   }
}

The output of the above code is −

BitArray 1: 1 0 1 1 
BitArray 2: 0 1 1 0 
After AND: 0 0 1 0 
After OR:  1 1 1 1 

Using BitArray SetAll and Not Operations

Example

using System;
using System.Collections;

public class Demo {
   public static void Main() {
      BitArray ba = new BitArray(6);
      
      // Set all bits to true
      ba.SetAll(true);
      Console.Write("After SetAll(true): ");
      for (int i = 0; i < ba.Count; i++) {
         Console.Write(ba[i] ? "1 " : "0 ");
      }
      Console.WriteLine();
      
      // Invert all bits
      ba.Not();
      Console.Write("After Not():        ");
      for (int i = 0; i < ba.Count; i++) {
         Console.Write(ba[i] ? "1 " : "0 ");
      }
      Console.WriteLine();
   }
}

The output of the above code is −

After SetAll(true): 1 1 1 1 1 1 
After Not():        0 0 0 0 0 0 

Conclusion

BitArray in C# provides an efficient way to store and manipulate bits with support for common bitwise operations like AND, OR, XOR, and NOT. It offers memory-efficient storage and indexer-based access, making it ideal for applications requiring boolean flags or bitwise computations.

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

235 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements