BitArray Class

Definition

Manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on (1) and false indicates the bit is off (0).

public ref class BitArray sealed : System::Collections::ICollection
public ref class BitArray sealed : ICloneable, System::Collections::ICollection
public sealed class BitArray : System.Collections.ICollection
public sealed class BitArray : ICloneable, System.Collections.ICollection
[System.Serializable]
public sealed class BitArray : ICloneable, System.Collections.ICollection
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class BitArray : ICloneable, System.Collections.ICollection
type BitArray = class
    interface ICollection
    interface IEnumerable
type BitArray = class
    interface ICollection
    interface IEnumerable
    interface ICloneable
[<System.Serializable>]
type BitArray = class
    interface ICollection
    interface IEnumerable
    interface ICloneable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type BitArray = class
    interface ICollection
    interface IEnumerable
    interface ICloneable
Public NotInheritable Class BitArray
Implements ICollection
Public NotInheritable Class BitArray
Implements ICloneable, ICollection
Inheritance
BitArray
Attributes
Implements

Examples

The following code example shows how to create and initialize a BitArray and how to print out its values.

using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable^ myList, int myWidth );
int main()
{
   
   // Creates and initializes several BitArrays.
   BitArray^ myBA1 = gcnew BitArray( 5 );
   BitArray^ myBA2 = gcnew BitArray( 5,false );
   array<Byte>^myBytes = {1,2,3,4,5};
   BitArray^ myBA3 = gcnew BitArray( myBytes );
   array<Boolean>^myBools = {true,false,true,true,false};
   BitArray^ myBA4 = gcnew BitArray( myBools );
   array<Int32>^myInts = {6,7,8,9,10};
   BitArray^ myBA5 = gcnew BitArray( myInts );
   
   // Displays the properties and values of the BitArrays.
   Console::WriteLine( "myBA1" );
   Console::WriteLine( "   Count:    {0}", myBA1->Count );
   Console::WriteLine( "   Length:   {0}", myBA1->Length );
   Console::WriteLine( "   Values:" );
   PrintValues( myBA1, 8 );
   Console::WriteLine( "myBA2" );
   Console::WriteLine( "   Count:    {0}", myBA2->Count );
   Console::WriteLine( "   Length:   {0}", myBA2->Length );
   Console::WriteLine( "   Values:" );
   PrintValues( myBA2, 8 );
   Console::WriteLine( "myBA3" );
   Console::WriteLine( "   Count:    {0}", myBA3->Count );
   Console::WriteLine( "   Length:   {0}", myBA3->Length );
   Console::WriteLine( "   Values:" );
   PrintValues( myBA3, 8 );
   Console::WriteLine( "myBA4" );
   Console::WriteLine( "   Count:    {0}", myBA4->Count );
   Console::WriteLine( "   Length:   {0}", myBA4->Length );
   Console::WriteLine( "   Values:" );
   PrintValues( myBA4, 8 );
   Console::WriteLine( "myBA5" );
   Console::WriteLine( "   Count:    {0}", myBA5->Count );
   Console::WriteLine( "   Length:   {0}", myBA5->Length );
   Console::WriteLine( "   Values:" );
   PrintValues( myBA5, 8 );
}

void PrintValues( IEnumerable^ myList, int myWidth )
{
   int i = myWidth;
   IEnumerator^ myEnum = myList->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      if ( i <= 0 )
      {
         i = myWidth;
         Console::WriteLine();
      }

      i--;
      Console::Write( "{0,8}", obj );
   }

   Console::WriteLine();
}

/* 
 This code produces the following output.
 
 myBA1
    Count:    5
    Length:   5
    Values:
    False   False   False   False   False
 myBA2
    Count:    5
    Length:   5
    Values:
    False   False   False   False   False
 myBA3
    Count:    40
    Length:   40
    Values:
     True   False   False   False   False   False   False   False
    False    True   False   False   False   False   False   False
     True    True   False   False   False   False   False   False
    False   False    True   False   False   False   False   False
     True   False    True   False   False   False   False   False
 myBA4
    Count:    5
    Length:   5
    Values:
     True   False    True    True   False
 myBA5
    Count:    160
    Length:   160
    Values:
    False    True    True   False   False   False   False   False
    False   False   False   False   False   False   False   False
    False   False   False   False   False   False   False   False
    False   False   False   False   False   False   False   False
     True    True    True   False   False   False   False   False
    False   False   False   False   False   False   False   False
    False   False   False   False   False   False   False   False
    False   False   False   False   False   False   False   False
    False   False   False    True   False   False   False   False
    False   False   False   False   False   False   False   False
    False   False   False   False   False   False   False   False
    False   False   False   False   False   False   False   False
     True   False   False    True   False   False   False   False
    False   False   False   False   False   False   False   False
    False   False   False   False   False   False   False   False
    False   False   False   False   False   False   False   False
    False    True   False    True   False   False   False   False
    False   False   False   False   False   False   False   False
    False   False   False   False   False   False   False   False
    False   False   False   False   False   False   False   False
 */
using System;
using System.Collections;
public class SamplesBitArray  {

   public static void Main()  {

      // Creates and initializes several BitArrays.
      BitArray myBA1 = new BitArray( 5 );

      BitArray myBA2 = new BitArray( 5, false );

      byte[] myBytes = new byte[5] { 1, 2, 3, 4, 5 };
      BitArray myBA3 = new BitArray( myBytes );

      bool[] myBools = new bool[5] { true, false, true, true, false };
      BitArray myBA4 = new BitArray( myBools );

      int[]  myInts  = new int[5] { 6, 7, 8, 9, 10 };
      BitArray myBA5 = new BitArray( myInts );

      // Displays the properties and values of the BitArrays.
      Console.WriteLine( "myBA1" );
      Console.WriteLine( "   Count:    {0}", myBA1.Count );
      Console.WriteLine( "   Length:   {0}", myBA1.Length );
      Console.WriteLine( "   Values:" );
      PrintValues( myBA1, 8 );

      Console.WriteLine( "myBA2" );
      Console.WriteLine( "   Count:    {0}", myBA2.Count );
      Console.WriteLine( "   Length:   {0}", myBA2.Length );
      Console.WriteLine( "   Values:" );
      PrintValues( myBA2, 8 );

      Console.WriteLine( "myBA3" );
      Console.WriteLine( "   Count:    {0}", myBA3.Count );
      Console.WriteLine( "   Length:   {0}", myBA3.Length );
      Console.WriteLine( "   Values:" );
      PrintValues( myBA3, 8 );

      Console.WriteLine( "myBA4" );
      Console.WriteLine( "   Count:    {0}", myBA4.Count );
      Console.WriteLine( "   Length:   {0}", myBA4.Length );
      Console.WriteLine( "   Values:" );
      PrintValues( myBA4, 8 );

      Console.WriteLine( "myBA5" );
      Console.WriteLine( "   Count:    {0}", myBA5.Count );
      Console.WriteLine( "   Length:   {0}", myBA5.Length );
      Console.WriteLine( "   Values:" );
      PrintValues( myBA5, 8 );
   }

   public static void PrintValues( IEnumerable myList, int myWidth )  {
      int i = myWidth;
      foreach ( Object obj in myList ) {
         if ( i <= 0 )  {
            i = myWidth;
            Console.WriteLine();
         }
         i--;
         Console.Write( "{0,8}", obj );
      }
      Console.WriteLine();
   }
}


/*
This code produces the following output.

myBA1
   Count:    5
   Length:   5
   Values:
   False   False   False   False   False
myBA2
   Count:    5
   Length:   5
   Values:
   False   False   False   False   False
myBA3
   Count:    40
   Length:   40
   Values:
    True   False   False   False   False   False   False   False
   False    True   False   False   False   False   False   False
    True    True   False   False   False   False   False   False
   False   False    True   False   False   False   False   False
    True   False    True   False   False   False   False   False
myBA4
   Count:    5
   Length:   5
   Values:
    True   False    True    True   False
myBA5
   Count:    160
   Length:   160
   Values:
   False    True    True   False   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False
    True    True    True   False   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False    True   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False
    True   False   False    True   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False
   False    True   False    True   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False
*/
Imports System.Collections

Public Class SamplesBitArray

    Public Shared Sub Main()

        ' Creates and initializes several BitArrays.
        Dim myBA1 As New BitArray(5)

        Dim myBA2 As New BitArray(5, False)

        Dim myBytes() As Byte = {1, 2, 3, 4, 5}
        Dim myBA3 As New BitArray(myBytes)

        Dim myBools() As Boolean = {True, False, True, True, False}
        Dim myBA4 As New BitArray(myBools)

        Dim myInts() As Integer = {6, 7, 8, 9, 10}
        Dim myBA5 As New BitArray(myInts)

        ' Displays the properties and values of the BitArrays.
        Console.WriteLine("myBA1")
        Console.WriteLine("   Count:    {0}", myBA1.Count)
        Console.WriteLine("   Length:   {0}", myBA1.Length)
        Console.WriteLine("   Values:")
        PrintValues(myBA1, 8)

        Console.WriteLine("myBA2")
        Console.WriteLine("   Count:    {0}", myBA2.Count)
        Console.WriteLine("   Length:   {0}", myBA2.Length)
        Console.WriteLine("   Values:")
        PrintValues(myBA2, 8)

        Console.WriteLine("myBA3")
        Console.WriteLine("   Count:    {0}", myBA3.Count)
        Console.WriteLine("   Length:   {0}", myBA3.Length)
        Console.WriteLine("   Values:")
        PrintValues(myBA3, 8)

        Console.WriteLine("myBA4")
        Console.WriteLine("   Count:    {0}", myBA4.Count)
        Console.WriteLine("   Length:   {0}", myBA4.Length)
        Console.WriteLine("   Values:")
        PrintValues(myBA4, 8)

        Console.WriteLine("myBA5")
        Console.WriteLine("   Count:    {0}", myBA5.Count)
        Console.WriteLine("   Length:   {0}", myBA5.Length)
        Console.WriteLine("   Values:")
        PrintValues(myBA5, 8)

    End Sub

    Public Shared Sub PrintValues(myList As IEnumerable, myWidth As Integer)
        Dim i As Integer = myWidth
        Dim obj As [Object]
        For Each obj In  myList
            If i <= 0 Then
                i = myWidth
                Console.WriteLine()
            End If
            i -= 1
            Console.Write("{0,8}", obj)
        Next obj
        Console.WriteLine()
    End Sub

End Class


' This code produces the following output.
' 
' myBA1
'    Count:    5
'    Length:   5
'    Values:
'    False   False   False   False   False
' myBA2
'    Count:    5
'    Length:   5
'    Values:
'    False   False   False   False   False
' myBA3
'    Count:    40
'    Length:   40
'    Values:
'     True   False   False   False   False   False   False   False
'    False    True   False   False   False   False   False   False
'     True    True   False   False   False   False   False   False
'    False   False    True   False   False   False   False   False
'     True   False    True   False   False   False   False   False
' myBA4
'    Count:    5
'    Length:   5
'    Values:
'     True   False    True    True   False
' myBA5
'    Count:    160
'    Length:   160
'    Values:
'    False    True    True   False   False   False   False   False
'    False   False   False   False   False   False   False   False
'    False   False   False   False   False   False   False   False
'    False   False   False   False   False   False   False   False
'     True    True    True   False   False   False   False   False
'    False   False   False   False   False   False   False   False
'    False   False   False   False   False   False   False   False
'    False   False   False   False   False   False   False   False
'    False   False   False    True   False   False   False   False
'    False   False   False   False   False   False   False   False
'    False   False   False   False   False   False   False   False
'    False   False   False   False   False   False   False   False
'     True   False   False    True   False   False   False   False
'    False   False   False   False   False   False   False   False
'    False   False   False   False   False   False   False   False
'    False   False   False   False   False   False   False   False
'    False    True   False    True   False   False   False   False
'    False   False   False   False   False   False   False   False
'    False   False   False   False   False   False   False   False
'    False   False   False   False   False   False   False   False

Remarks

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. The size of a BitArray is controlled by the client; indexing past the end of the BitArray throws an ArgumentException. The BitArray class provides methods that are not found in other collections, including those that allow multiple elements to be modified at once using a filter, such as And, Or, Xor , Not, and SetAll.

The BitVector32 class is a structure that provides the same functionality as BitArray, but with faster performance. BitVector32 is faster because it is a value type and therefore allocated on the stack, whereas BitArray is a reference type and, therefore, allocated on the heap.

System.Collections.Specialized.BitVector32 can store exactly 32 bits, whereas BitArray can store a variable number of bits. BitVector32 stores both bit flags and small integers, thereby making it ideal for data that is not exposed to the user. However, if the number of required bit flags is unknown, is variable, or is greater than 32, use BitArray instead.

BitArray is in the System.Collections namespace; BitVector32 is in the System.Collections.Specialized namespace.

Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.

Constructors

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.

Properties

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.

Methods

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.

(Inherited from 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.

GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
HasAllSet()

Determines whether all bits in the BitArray are set to true.

HasAnySet()

Determines whether any bit in the BitArray is set to true.

LeftShift(Int32)

Shifts all the bit values of the current BitArray to the left on count bits.

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from 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.

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.

RightShift(Int32)

Shifts all the bit values of the current BitArray to the right on count bits.

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.

(Inherited from 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.

Explicit Interface Implementations

ICollection.CopyTo(Array, Int32)

Copies the elements of the BitArray to an Array, starting at the specified Array index.

ICollection.Count

Gets the number of elements in the BitArray.

ICollection.IsSynchronized

Gets a value that indicates whether access to the BitArray is synchronized (thread safe).

ICollection.SyncRoot

Gets an object that can be used to synchronize access to the BitArray.

Extension Methods

Cast<TResult>(IEnumerable)

Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

Enables parallelization of a query.

AsQueryable(IEnumerable)

Converts an IEnumerable to an IQueryable.

Applies to

Thread Safety

Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

This implementation does not provide a synchronized (thread safe) wrapper for a BitArray.

Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

See also