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

System.Object
  System.Collections.BitArray

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

[SerializableAttribute]
[ComVisibleAttribute(true)]
public sealed class BitArray : ICollection, 
	IEnumerable, ICloneable

The BitArray type exposes the following members.

  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsBitArray(BitArray)Initializes a new instance of the BitArray class that contains bit values copied from the specified BitArray.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsBitArray(Boolean[])Initializes a new instance of the BitArray class that contains bit values copied from the specified array of Booleans.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsBitArray(Byte[])Initializes a new instance of the BitArray class that contains bit values copied from the specified array of bytes.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsBitArray(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 methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsBitArray(Int32[])Initializes a new instance of the BitArray class that contains bit values copied from the specified array of 32-bit integers.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsBitArray(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

  NameDescription
Public propertySupported by the XNA FrameworkCountGets the number of elements contained in the BitArray.
Public propertySupported by the XNA FrameworkIsReadOnlyGets a value indicating whether the BitArray is read-only.
Public propertySupported by the XNA FrameworkIsSynchronizedGets a value indicating whether access to the BitArray is synchronized (thread safe).
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsItemGets or sets the value of the bit at a specific position in the BitArray.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsLengthGets or sets the number of elements in the BitArray.
Public propertySupported by the XNA FrameworkSyncRootGets an object that can be used to synchronize access to the BitArray.
Top

  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsAndPerforms the bitwise AND operation on the elements in the current BitArray against the corresponding elements in the specified BitArray.
Public methodSupported by the XNA FrameworkCloneCreates a shallow copy of the BitArray.
Public methodSupported by the XNA FrameworkCopyToCopies the entire BitArray to a compatible one-dimensional Array, starting at the specified index of the target array.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetGets the value of the bit at a specific position in the BitArray.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetEnumeratorReturns an enumerator that iterates through the BitArray.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsNotInverts 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 methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsOrPerforms the bitwise OR operation on the elements in the current BitArray against the corresponding elements in the specified BitArray.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsSetSets the bit at a specific position in the BitArray to the specified value.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsSetAllSets all bits in the BitArray to the specified value.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsXorPerforms the bitwise exclusive OR operation on the elements in the current BitArray against the corresponding elements in the specified BitArray.
Top

  NameDescription
Public Extension MethodSupported by Portable Class LibrarySupported in .NET for Windows Store appsAsParallelEnables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension MethodSupported by Portable Class LibrarySupported in .NET for Windows Store appsAsQueryableConverts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension MethodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsCast<TResult>Casts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension MethodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsOfType<TResult>Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Top

  NameDescription
Explicit interface implemetationPrivate methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsICollection.CopyToCopies the elements of the BitArray to an Array, starting at the specified Array index.
Explicit interface implemetationPrivate propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsICollection.CountGets the number of elements in the BitArray.
Explicit interface implemetationPrivate propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsICollection.IsSynchronizedGets a value that indicates whether access to the BitArray is synchronized (thread safe).
Explicit interface implemetationPrivate propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsICollection.SyncRootGets an object that can be used to synchronize access to the BitArray.
Top

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.

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

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

.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

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), 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.

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.

Show:
© 2014 Microsoft