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

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

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class BitArray sealed : ICollection, IEnumerable, ICloneable
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class BitArray implements ICollection, IEnumerable, 
	ICloneable
SerializableAttribute 
ComVisibleAttribute(true) 
public final class BitArray implements ICollection, IEnumerable, 
	ICloneable
Not applicable.

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.

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

import System.*;
import System.Collections.*;

public class SamplesBitArray
{
    public static void main(String[] args)
    {
        // Creates and initializes several BitArrays.
        BitArray myBA1 = new BitArray(5);
        BitArray myBA2 = new BitArray(5, false);
        ubyte myBytes[] = new ubyte[] { 1, 2, 3, 4, 5 };
        BitArray myBA3 = new BitArray(myBytes);
        boolean myBools[] = new boolean[] { true, false, true, true, false };
        BitArray myBA4 = new BitArray(myBools);
        int myInts[] = new int[] { 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}",(Int32)myBA1.get_Count());
        Console.WriteLine("   Length:   {0}",(Int32)myBA1.get_Length());
        Console.WriteLine("   Values:");
        PrintValues(myBA1, 8);
        Console.WriteLine("myBA2");
        Console.WriteLine("   Count:    {0}",(Int32)myBA2.get_Count());
        Console.WriteLine("   Length:   {0}",(Int32)myBA2.get_Length());
        Console.WriteLine("   Values:");
        PrintValues(myBA2, 8);
        Console.WriteLine("myBA3");
        Console.WriteLine("   Count:    {0}",(Int32)myBA3.get_Count());
        Console.WriteLine("   Length:   {0}", (Int32)myBA3.get_Length());
        Console.WriteLine("   Values:");
        PrintValues(myBA3, 8);
        Console.WriteLine("myBA4");
        Console.WriteLine("   Count:    {0}", (Int32)myBA4.get_Count());
        Console.WriteLine("   Length:   {0}", (Int32)myBA4.get_Length());
        Console.WriteLine("   Values:");
        PrintValues(myBA4, 8);
        Console.WriteLine("myBA5");
        Console.WriteLine("   Count:    {0}",(Int32)myBA5.get_Count());
        Console.WriteLine("   Length:   {0}", (Int32)myBA5.get_Length());
        Console.WriteLine("   Values:");
        PrintValues(myBA5, 8);
    } //main

    public static void PrintValues(IEnumerable myList, int myWidth)
    {
        int i = myWidth;
        IEnumerator objMyEnum = myList.GetEnumerator();
        while (objMyEnum.MoveNext()) {
            Object obj = objMyEnum.get_Current();
            if (i <= 0) {
                i = myWidth;
                Console.WriteLine();
            }
            i--;
            Console.Write("{0,8}", obj);
        }
        Console.WriteLine();
    } //PrintValues
} //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
 */


System.Object
  System.Collections.BitArray

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.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft