Copying BitArray elements to an Array in C#

The BitArray class in C# provides the CopyTo() method to copy its elements to a compatible one-dimensional array. This method is useful when you need to transfer bit values from a BitArray to a regular array for further processing or storage.

Syntax

Following is the syntax for copying BitArray elements to an array −

bitArray.CopyTo(array, arrayIndex);

Parameters

  • array − The target one-dimensional array to copy elements to. Must be compatible with BitArray elements (bool[], int[], or byte[]).

  • arrayIndex − The zero-based index in the target array at which copying begins.

Using CopyTo() with Boolean Array

The most common approach is copying BitArray elements to a boolean array −

using System;
using System.Collections;

public class Demo {
    public static void Main() {
        BitArray arr = new BitArray(3);
        arr[0] = false;
        arr[1] = true;
        arr[2] = false;
        
        Console.WriteLine("Elements in BitArray...");
        foreach (bool res in arr) {
            Console.WriteLine(res);
        }
        
        bool[] boolArr = new bool[3];
        arr.CopyTo(boolArr, 0);
        
        Console.WriteLine("\nArray after copying...");
        foreach (bool obj in boolArr) {
            Console.WriteLine(obj);
        }
    }
}

The output of the above code is −

Elements in BitArray...
False
True
False

Array after copying...
False
True
False

Using CopyTo() with Offset

You can specify a starting index in the target array to copy BitArray elements at a specific position −

using System;
using System.Collections;

public class Demo {
    public static void Main() {
        BitArray arr = new BitArray(4);
        arr[0] = true;
        arr[1] = false;
        arr[2] = true;
        arr[3] = false;
        
        Console.WriteLine("Elements in BitArray...");
        foreach (bool res in arr) {
            Console.WriteLine(res);
        }
        
        bool[] boolArr = new bool[7];
        // Initialize first 3 elements
        boolArr[0] = false;
        boolArr[1] = false;
        boolArr[2] = false;
        
        // Copy BitArray starting at index 2
        arr.CopyTo(boolArr, 2);
        
        Console.WriteLine("\nArray after copying with offset...");
        for (int i = 0; i 

The output of the above code is −

Elements in BitArray...
True
False
True
False

Array after copying with offset...
Index 0: False
Index 1: False
Index 2: True
Index 3: False
Index 4: True
Index 5: False
Index 6: False

Using CopyTo() with Integer Array

BitArray elements can also be copied to an integer array, where false becomes 0 and true becomes 1 −

using System;
using System.Collections;

public class Demo {
    public static void Main() {
        BitArray arr = new BitArray(5);
        arr[0] = true;
        arr[1] = false;
        arr[2] = true;
        arr[3] = true;
        arr[4] = false;
        
        Console.WriteLine("Elements in BitArray...");
        foreach (bool res in arr) {
            Console.WriteLine(res);
        }
        
        int[] intArr = new int[5];
        arr.CopyTo(intArr, 0);
        
        Console.WriteLine("\nInteger array after copying...");
        foreach (int val in intArr) {
            Console.WriteLine(val);
        }
    }
}

The output of the above code is −

Elements in BitArray...
True
False
True
True
False

Integer array after copying...
1
0
1
1
0

Conclusion

The CopyTo() method provides an efficient way to transfer BitArray elements to compatible arrays like bool[], int[], or byte[]. You can specify an offset to control where the copying begins in the target array, making it flexible for various data manipulation scenarios.

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

311 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements