Open In App

C# | BitArray Class

Last Updated : 03 Apr, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The BitArray class 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. This class is contained in System.Collections namespace.

Properties of BitArray Class:

  • The BitArray class is a collection class in which the capacity is always the same as the count.
  • Elements are added to a BitArray by increasing the Length property.
  • Elements are deleted by decreasing the Length property.
  • Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.

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:




// C# code to create a BitArray
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a BitArray
        BitArray myBitArr = new BitArray(5);
  
        myBitArr[0] = true;
        myBitArr[1] = true;
        myBitArr[2] = false;
        myBitArr[3] = true;
        myBitArr[4] = false;
  
        // To get the value of index at index 2
        Console.WriteLine(myBitArr.Get(2));
  
        // To get the value of index at index 3
        Console.WriteLine(myBitArr.Get(3));
    }
}


Output:

False
True

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:




// C# program to illustrate the 
// BitArray Class Properties
using System; 
using System.Collections; 
  
class GFG { 
    
    // Driver code 
    public static void Main() 
    
    
        // Creating a BitArray 
        BitArray myBitArr = new BitArray(new byte[] { 0, 0, 0, 1 }); 
    
        // -------- IsReadOnly Property --------
          
        // Checking if the BitArray is read-only 
        Console.WriteLine(myBitArr.IsReadOnly); 
          
        // -------- Count Property --------
          
        // To get the number of elements 
        // contained in the BitArray 
        Console.WriteLine(myBitArr.Count); 
          
    


Output:

False
32

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:




// C# code to do bitwise
// OR between BitArray
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a BitArray
        BitArray myBitArr1 = new BitArray(4);
  
        // Creating a BitArray
        BitArray myBitArr2 = new BitArray(4);
  
        // Initializing values in myBitArr1
        myBitArr1[0] = false;
        myBitArr1[1] = false;
        myBitArr1[2] = true;
        myBitArr1[3] = true;
  
        // Initializing values in myBitArr2
        myBitArr2[0] = false;
        myBitArr2[2] = false;
        myBitArr2[1] = true;
        myBitArr2[3] = true;
  
        // function calling
        PrintValues(myBitArr1.Or(myBitArr2));
    }
  
    // Displaying the result
    public static void PrintValues(IEnumerable myList)
    {
        foreach(Object obj in myList)
        {
            Console.WriteLine(obj);
        }
    }
}


Output:

False
True
True
True

Example 2:




// C# code to set all bits in the
// BitArray to the specified value
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a BitArray myBitArr
        BitArray myBitArr = new BitArray(5);
  
        // Initializing all the bits in myBitArr
        myBitArr[0] = false;
        myBitArr[1] = true;
        myBitArr[2] = true;
        myBitArr[3] = false;
        myBitArr[4] = true;
  
        // Printing the values in myBitArr
        Console.WriteLine("Initially the bits are as : ");
  
        PrintIndexAndValues(myBitArr);
  
        // Setting all bits to false
        myBitArr.SetAll(false);
  
        // Printing the values in myBitArr
        // It should display all the bits as false
        Console.WriteLine("Finally the bits are as : ");
  
        PrintIndexAndValues(myBitArr);
    }
  
    // Function to display bits
    public static void PrintIndexAndValues(IEnumerable myArr)
    {
        foreach(Object obj in myArr)
        {
            Console.WriteLine(obj);
        }
    }
}


Output:

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

Reference:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads