C# | BitArray Class - GeeksforGeeks

03 Apr, 2019
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.
Example:
// C# code to create a BitArrayusing 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

Example:
// C# program to illustrate the // BitArray Class Propertiesusing 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

Example 1:
// C# code to do bitwise// OR between BitArrayusing 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 valueusing 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:
https://docs.microsoft.com/en-us/dotnet/api/system.collections.bitarray?view=netframework-4.7.2
CSharp-Collections-BitArray
CSharp-Collections-Namespace
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# Dictionary with examples
C# | Delegates
C# | Method Overriding
C# | Abstract Classes
Difference between Ref and Out keywords in C#
C# | Class and Object
C# | Constructors
Extension Method in C#
Introduction to .NET Framework
C# | String.IndexOf( ) Method | Set - 1