BitArray Class (System.Collections)

Switch View :
ScriptFree
.NET Framework Class Library
BitArray Class

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

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).

Inheritance Hierarchy

System.Object
  System.Collections.BitArray

Namespace:  System.Collections
Assembly:  mscorlib (in mscorlib.dll)
Syntax

Visual Basic
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class BitArray _
	Implements ICollection, IEnumerable, ICloneable
C#
[SerializableAttribute]
[ComVisibleAttribute(true)]
public sealed class BitArray : ICollection, 
	IEnumerable, ICloneable
Visual C++
[SerializableAttribute]
[ComVisibleAttribute(true)]
public ref class BitArray sealed : ICollection, 
	IEnumerable, ICloneable
F#
[<Sealed>]
[<SerializableAttribute>]
[<ComVisibleAttribute(true)>]
type BitArray =  
    class
        interface ICollection
        interface IEnumerable
        interface ICloneable
    end

The BitArray type exposes the following members.

Constructors

  Name Description
Public method Supported by the XNA Framework Supported by Portable Class Library BitArray(BitArray) Initializes a new instance of the BitArray class that contains bit values copied from the specified BitArray.
Public method Supported by the XNA Framework Supported by Portable Class Library BitArray(Boolean[]) Initializes a new instance of the BitArray class that contains bit values copied from the specified array of Booleans.
Public method Supported by the XNA Framework Supported by Portable Class Library BitArray(Byte[]) Initializes a new instance of the BitArray class that contains bit values copied from the specified array of bytes.
Public method Supported by the XNA Framework Supported by Portable Class Library 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.
Public method Supported by the XNA Framework Supported by Portable Class Library BitArray(Int32[]) Initializes a new instance of the BitArray class that contains bit values copied from the specified array of 32-bit integers.
Public method Supported by the XNA Framework Supported by Portable Class Library 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.
Top
Properties

  Name Description
Public property Supported by the XNA Framework Count Gets the number of elements contained in the BitArray.
Public property Supported by the XNA Framework IsReadOnly Gets a value indicating whether the BitArray is read-only.
Public property Supported by the XNA Framework IsSynchronized Gets a value indicating whether access to the BitArray is synchronized (thread safe).
Public property Supported by the XNA Framework Supported by Portable Class Library Item Gets or sets the value of the bit at a specific position in the BitArray.
Public property Supported by the XNA Framework Supported by Portable Class Library Length Gets or sets the number of elements in the BitArray.
Public property Supported by the XNA Framework SyncRoot Gets an object that can be used to synchronize access to the BitArray.
Top
Methods

  Name Description
Public method Supported by the XNA Framework Supported by Portable Class Library And Performs the bitwise AND operation on the elements in the current BitArray against the corresponding elements in the specified BitArray.
Public method Supported by the XNA Framework Clone Creates a shallow copy of the BitArray.
Public method Supported by the XNA Framework CopyTo Copies the entire BitArray to a compatible one-dimensional Array, starting at the specified index of the target array.
Public method Supported by the XNA Framework Supported by Portable Class Library Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Supported by the XNA Framework Supported by Portable Class Library Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method Supported by the XNA Framework Supported by Portable Class Library Get Gets the value of the bit at a specific position in the BitArray.
Public method Supported by the XNA Framework Supported by Portable Class Library GetEnumerator Returns an enumerator that iterates through the BitArray.
Public method Supported by the XNA Framework Supported by Portable Class Library GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method Supported by the XNA Framework Supported by Portable Class Library GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method Supported by the XNA Framework Supported by Portable Class Library MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Supported by the XNA Framework Supported by Portable Class Library 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.
Public method Supported by the XNA Framework Supported by Portable Class Library Or Performs the bitwise OR operation on the elements in the current BitArray against the corresponding elements in the specified BitArray.
Public method Supported by the XNA Framework Supported by Portable Class Library Set Sets the bit at a specific position in the BitArray to the specified value.
Public method Supported by the XNA Framework Supported by Portable Class Library SetAll Sets all bits in the BitArray to the specified value.
Public method Supported by the XNA Framework Supported by Portable Class Library ToString Returns a string that represents the current object. (Inherited from Object.)
Public method Supported by the XNA Framework Supported by Portable Class Library Xor Performs the bitwise exclusive OR operation on the elements in the current BitArray against the corresponding elements in the specified BitArray.
Top
Extension Methods

  Name Description
Public Extension Method AsParallel Enables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension Method Supported by Portable Class Library AsQueryable Converts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension Method Supported by the XNA Framework Supported by Portable Class Library Cast<TResult> Casts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension Method Supported by the XNA Framework Supported by Portable Class Library OfType<TResult> Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Top
Explicit Interface Implementations

  Name Description
Explicit interface implemetation Private method Supported by Portable Class Library ICollection.CopyTo Copies the elements of the BitArray to an Array, starting at the specified Array index.
Explicit interface implemetation Private property Supported by Portable Class Library ICollection.Count Gets the number of elements in the BitArray.
Explicit interface implemetation Private property Supported by Portable Class Library ICollection.IsSynchronized Gets a value that indicates whether access to the BitArray is synchronized (thread safe).
Explicit interface implemetation Private property Supported by Portable Class Library ICollection.SyncRoot Gets an object that can be used to synchronize access to the BitArray.
Top
Remarks

The size of a BitArray is controlled by the client; indexing past the end of the BitArray throws an ArgumentException.

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

Examples

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

Visual Basic

Imports System
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 'Main

    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 'PrintValues

End Class 'SamplesBitArray 


' 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



C#

 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
 */ 



Visual C++

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
 */


Version Information

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library
Platforms

Windows 8 Consumer Preview, Windows Server 8 Beta, Windows 7, Windows Server 2008 SP2, Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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

Reference