A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/c-sharp/c-sharp-bitarray-class/ below:

C# BitArray Class - GeeksforGeeks

C# BitArray Class

Last Updated : 11 Jul, 2025

BitArray class in C# is part of the System.Collections namespace. It manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on i.e. 1, and false indicates the bit is off i.e. 0. 

Example: This example demonstrates how to create a BitArray, set individual bits to true, and print the bit value at each index.

C#
using System;
using System.Collections;

class Geeks {
    static void Main(string[] args)
    {
        // Creating a BitArray with 5 bits (initialized to
        // false by default)
        BitArray b = new BitArray(5);

        // Setting individual bits
        b[0] = true;
        b[1] = true;
        b[3] = true;

        // Printing the BitArray (will display true or false
        // for each bit)
        for (int i = 0; i < b.Count; i++) {
            Console.WriteLine(
                $"Bit at index {i}: {b[i]}");
        }
    }
}

Output
Bit at index 0: True
Bit at index 1: True
Bit at index 2: False
Bit at index 3: True
Bit at index 4: False
Declaration of BitArray

In C#, the bitArray is declared as:

BitArray bitArray = new BitArray(size);

Constructors Constructor Description BitArray(BitArray) Initializes a new instance of the BitArray class that contains bit values copied from the specified BitArray. BitArray(Boolean[]) Initializes a new instance of the BitArray class that contains bit values copied from the specified array of Booleans. BitArray(Byte[]) Initializes a new instance of the BitArray class that contains bit values copied from the specified array of bytes. BitArray(Int32) Initializes a new instance of the BitArray class that can hold the specified number of bit values, which are initially set to false. BitArray(Int32, Boolean) Initializes a new instance of the BitArray class that can hold the specified number of bit values, which are initially set to the specified value. BitArray(Int32[]) Initializes a new instance of the BitArray class that contains bit values copied from the specified array of 32-bit integers.

Example: This example demonstrates how to create a BitArray, set individual bit values and reterive specific bit values using the Get() method.

C#
// C# Program to get a BitArray 
using System; 
using System.Collections; 

class Geeks { 
	public static void Main() 
	{ 

		// Creating a BitArray 
		BitArray b = new BitArray(5); 

		b[0] = true; 
		b[1] = true; 
		b[2] = false; 
		b[3] = true; 
		b[4] = false; 

		// To get the value of index at index 2 
		Console.WriteLine(b.Get(2)); 

		// To get the value of index at index 3 
		Console.WriteLine(b.Get(3)); 
	} 
} 
Properties Property Description Count Gets the number of elements contained in the BitArray. IsReadOnly Gets a value indicating whether the BitArray is read-only. IsSynchronized Gets a value indicating whether access to the BitArray is synchronized (thread safe). Item[Int32] Gets or sets the value of the bit at a specific position in the BitArray. Length Gets or sets the number of elements in the BitArray. SyncRoot Gets an object that can be used to synchronize access to the BitArray.

Example: This example demonstrates checking if a BitArray is read-only using the IsReadOnly property and getting the number of elements using the Count property.

C#
// C# program to demonstrates the 
// IsReadOnly and Count property
using System; 
using System.Collections; 

class Geeks { 
	
	public static void Main() 
	{ 
	
		// Creating a BitArray 
		BitArray b = new BitArray(new byte[] { 0, 0, 0, 1 }); 
	
		// IsReadOnly Property 
		
		// Checking if the BitArray is read-only 
		Console.WriteLine(b.IsReadOnly); 
		
		// Count Property
		
		// To get the number of elements 
		// contained in the BitArray 
		Console.WriteLine(b.Count); 
		
	} 
} 
Methods Method Description And(BitArray) Performs the bitwise AND operation between the elements of the current BitArray object and the corresponding elements in the specified array. The current BitArray object will be modified to store the result of the bitwise AND operation. Clone() Creates a shallow copy of the BitArray. CopyTo(Array, Int32) Copies the entire BitArray to a compatible one-dimensional Array, starting at the specified index of the target array. Equals(Object) Determines whether the specified object is equal to the current object. Get(Int32) Gets the value of the bit at a specific position in the BitArray. GetEnumerator() Returns an enumerator that iterates through the BitArray. LeftShift(Int32) It is used to shift the bits of the bit array to the left by one position and adds zeros on the shifted position. GetHashCode() Serves as the default hash function. GetType() Gets the Type of the current instance. MemberwiseClone() Creates a shallow copy of the current Object. 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. RightShift(Int32) It is used to shift the bits of the bit array to the right by one position and adds zeros on the shifted position. Or(BitArray) Performs the bitwise OR operation between the elements of the current BitArray object and the corresponding elements in the specified array. The current BitArray object will be modified to store the result of the bitwise OR operation. Set(Int32, Boolean) Sets the bit at a specific position in the BitArray to the specified value. SetAll(Boolean) Sets all bits in the BitArray to the specified value. ToString() Returns a string that represents the current object. Xor(BitArray) Performs the bitwise exclusive OR operation between the elements of the current BitArray object against the corresponding elements in the specified array. The current BitArray object will be modified to store the result of the bitwise exclusive OR operation.

Example 1: This example demonstrates performing a bitwise OR operation between two BitArray objects and printing the resulting bit values.

C#
// C# Program to do bitwise 
// OR operation between BitArray 
using System; 
using System.Collections; 

class Geeks { 

	public static void Main() 
	{ 

		// Creating a BitArray 
		BitArray b1 = new BitArray(4); 

		// Creating a BitArray 
		BitArray b2 = new BitArray(4); 

		// Initializing values in b1 
		b1[0] = false; 
		b1[1] = false; 
		b1[2] = true; 
		b1[3] = true; 

		// Initializing values in b2 
		b2[0] = false; 
		b2[2] = false; 
		b2[1] = true; 
		b2[3] = true; 

		// function calling 
		PrintValues(b1.Or(b2)); 
	} 

	// Displaying the result 
	public static void PrintValues(IEnumerable myList) 
	{ 
		foreach(Object o in myList) 
		{ 
			Console.WriteLine(o); 
		} 
	} 
} 

Output
False
True
True
True

Example 2: This example demonstrates setting all bits in a BitArray to false using the SetAll() method and printing the bit values before and after.

C#
// C# code to set all bits in the 
// BitArray to the specified value 
using System;
using System.Collections;

class Geeks {

    public static void Main()
    {

        // Creating a BitArray b 
        BitArray b = new BitArray(5);

        // Initializing all the bits in a 
        b[0] = false;
        b[1] = true;
        b[2] = true;
        b[3] = false;
        b[4] = true;

        // Printing the values in b 
        Console.WriteLine("Initially the bits are as: ");

        PrintIndexAndValues(b);

        // Setting all bits to false 
        b.SetAll(false);

        // Printing the values in b 
        // It should display all the bits as false 
        Console.WriteLine("Finally the bits are as: ");

        PrintIndexAndValues(b);
    }

    // Function to display bits 
    public static void PrintIndexAndValues(IEnumerable myArr)
    {
        foreach(Object o in myArr)
        {
            Console.WriteLine(o);
        }
    }
} 

Output
Initially the bits are as: 
False
True
True
False
True
Finally the bits are as: 
False
False
False
False
False


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4