Export (0) Print
Expand All

BitArray Class

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

For a list of all members of this type, see BitArray Members.

System.Object
   System.Collections.BitArray

[Visual Basic]
<Serializable>
NotInheritable Public Class BitArray
   Implements ICollection, IEnumerable, ICloneable
[C#]
[Serializable]
public sealed class BitArray : ICollection, IEnumerable,
   ICloneable
[C++]
[Serializable]
public __gc __sealed class BitArray : public ICollection,
   IEnumerable, ICloneable
[JScript]
public
   Serializable
class BitArray implements ICollection, IEnumerable,
   ICloneable

Thread Safety

Public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. 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 could 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.

Remarks

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

Indexes in this collection are zero-based.

Example

[Visual Basic, C#, C++] The following example shows how to create and initialize a BitArray and how to print out its values.

[Visual Basic] 
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

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(ControlChars.Tab + "Count:    {0}", myBA1.Count)
        Console.WriteLine(ControlChars.Tab + "Length:   {0}", myBA1.Length)
        Console.WriteLine(ControlChars.Tab + "Values:")
        PrintValues(myBA1, 8)
        
        Console.WriteLine("myBA2")
        Console.WriteLine(ControlChars.Tab + "Count:    {0}", myBA2.Count)
        Console.WriteLine(ControlChars.Tab + "Length:   {0}", myBA2.Length)
        Console.WriteLine(ControlChars.Tab + "Values:")
        PrintValues(myBA2, 8)
        
        Console.WriteLine("myBA3")
        Console.WriteLine(ControlChars.Tab + "Count:    {0}", myBA3.Count)
        Console.WriteLine(ControlChars.Tab + "Length:   {0}", myBA3.Length)
        Console.WriteLine(ControlChars.Tab + "Values:")
        PrintValues(myBA3, 8)
        
        Console.WriteLine("myBA4")
        Console.WriteLine(ControlChars.Tab + "Count:    {0}", myBA4.Count)
        Console.WriteLine(ControlChars.Tab + "Length:   {0}", myBA4.Length)
        Console.WriteLine(ControlChars.Tab + "Values:")
        PrintValues(myBA4, 8)
        
        Console.WriteLine("myBA5")
        Console.WriteLine(ControlChars.Tab + "Count:    {0}", myBA5.Count)
        Console.WriteLine(ControlChars.Tab + "Length:   {0}", myBA5.Length)
        Console.WriteLine(ControlChars.Tab + "Values:")
        PrintValues(myBA5, 8)
    End Sub
    
    Public Shared Sub PrintValues(myList As IEnumerable, myWidth As Integer)
        Dim myEnumerator As System.Collections.IEnumerator = _
           myList.GetEnumerator()
        Dim i As Integer = myWidth
        While myEnumerator.MoveNext()
            If i <= 0 Then
                i = myWidth
                Console.WriteLine()
            End If
            i -= 1
            Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
        End While
        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

[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( "\tCount:    {0}", myBA1.Count );
      Console.WriteLine( "\tLength:   {0}", myBA1.Length );
      Console.WriteLine( "\tValues:" );
      PrintValues( myBA1, 8 );

      Console.WriteLine( "myBA2" );
      Console.WriteLine( "\tCount:    {0}", myBA2.Count );
      Console.WriteLine( "\tLength:   {0}", myBA2.Length );
      Console.WriteLine( "\tValues:" );
      PrintValues( myBA2, 8 );

      Console.WriteLine( "myBA3" );
      Console.WriteLine( "\tCount:    {0}", myBA3.Count );
      Console.WriteLine( "\tLength:   {0}", myBA3.Length );
      Console.WriteLine( "\tValues:" );
      PrintValues( myBA3, 8 );

      Console.WriteLine( "myBA4" );
      Console.WriteLine( "\tCount:    {0}", myBA4.Count );
      Console.WriteLine( "\tLength:   {0}", myBA4.Length );
      Console.WriteLine( "\tValues:" );
      PrintValues( myBA4, 8 );

      Console.WriteLine( "myBA5" );
      Console.WriteLine( "\tCount:    {0}", myBA5.Count );
      Console.WriteLine( "\tLength:   {0}", myBA5.Length );
      Console.WriteLine( "\tValues:" );
      PrintValues( myBA5, 8 );
   }


   public static void PrintValues( IEnumerable myList, int myWidth )  {
      System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
      int i = myWidth;
      while ( myEnumerator.MoveNext() )  {
         if ( i <= 0 )  {
            i = myWidth;
            Console.WriteLine();
         }
         i--;
         Console.Write( "\t{0}", myEnumerator.Current );
      }
      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
*/ 

[C++] 
#using <mscorlib.dll>
#using <system.dll>

using namespace System;
using namespace System::Collections;


void PrintValues( IEnumerable* myList, int myWidth )  {
   System::Collections::IEnumerator* myEnumerator = myList->GetEnumerator();
   int i = myWidth;
   while ( myEnumerator->MoveNext() )  {
      if ( i <= 0 )  {
         i = myWidth;
         Console::WriteLine();
      }
      i--;
      Console::Write( S"\t{0}", myEnumerator->Current );
   }
   Console::WriteLine();
}

int main()  {

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

   BitArray* myBA2 = new BitArray( 5, false );

   Byte myBytes[] = new Byte[5];
   for( int i(0); i < myBytes->Length; ++i ) {
      myBytes[i] = i+1;
   }

   BitArray* myBA3 = new BitArray( myBytes );

   bool myBools __gc[] = new bool __gc[5];
   for( int i(0); i < myBools->Length; ++i ) {
      myBools[i] = (i % 2 == 0);
   }

   BitArray* myBA4 = new BitArray( myBools );

   int myInts __gc[] = new int __gc[5];
   for( int i(0); i < myInts->Length; ++i ) {
      myInts[i] = i+6;
   }

   BitArray* myBA5 = new BitArray( myInts );

   // Displays the properties and values of the BitArrays.
   Console::WriteLine( S"myBA1" );
   Console::WriteLine( S"\tCount:    {0}", __box(myBA1->Count) );
   Console::WriteLine( S"\tLength:   {0}", __box(myBA1->Length) );
   Console::WriteLine( S"\tValues:" );
   PrintValues( myBA1, 8 );

   Console::WriteLine( S"myBA2" );
   Console::WriteLine( S"\tCount:    {0}", __box(myBA2->Count) );
   Console::WriteLine( S"\tLength:   {0}", __box(myBA2->Length) );
   Console::WriteLine( S"\tValues:" );
   PrintValues( myBA2, 8 );

   Console::WriteLine( S"myBA3" );
   Console::WriteLine( S"\tCount:    {0}", __box(myBA3->Count) );
   Console::WriteLine( S"\tLength:   {0}", __box(myBA3->Length) );
   Console::WriteLine( S"\tValues:" );
   PrintValues( myBA3, 8 );

   Console::WriteLine( S"myBA4" );
   Console::WriteLine( S"\tCount:    {0}", __box(myBA4->Count) );
   Console::WriteLine( S"\tLength:   {0}", __box(myBA4->Length) );
   Console::WriteLine( S"\tValues:" );
   PrintValues( myBA4, 8 );

   Console::WriteLine( S"myBA5" );
   Console::WriteLine( S"\tCount:    {0}", __box(myBA5->Count) );
   Console::WriteLine( S"\tLength:   {0}", __box(myBA5->Length) );
   Console::WriteLine( S"\tValues:" );
   PrintValues( myBA5, 8 );
}

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

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Collections

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

BitArray Members | System.Collections Namespace

Show:
© 2014 Microsoft