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)

System.Object
  System.Collections.BitArray

<SerializableAttribute>
<ComVisibleAttribute(True)>
Public NotInheritable Class BitArray
	Implements ICollection, IEnumerable, ICloneable

NameDescription
System_CAPS_pubmethodBitArray(BitArray)

Initializes a new instance of the BitArray class that contains bit values copied from the specified BitArray.

System_CAPS_pubmethodBitArray(Boolean())

Initializes a new instance of the BitArray class that contains bit values copied from the specified array of Booleans.

System_CAPS_pubmethodBitArray(Byte())

Initializes a new instance of the BitArray class that contains bit values copied from the specified array of bytes.

System_CAPS_pubmethodBitArray(Int32)

Initializes a new instance of the BitArray class that can hold the specified number of bit values, which are initially set to false.

System_CAPS_pubmethodBitArray(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.

System_CAPS_pubmethodBitArray(Int32())

Initializes a new instance of the BitArray class that contains bit values copied from the specified array of 32-bit integers.

NameDescription
System_CAPS_pubpropertyCount

Gets the number of elements contained in the BitArray.

System_CAPS_pubpropertyIsReadOnly

Gets a value indicating whether the BitArray is read-only.

System_CAPS_pubpropertyIsSynchronized

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

System_CAPS_pubpropertyItem(Int32)

Gets or sets the value of the bit at a specific position in the BitArray.

System_CAPS_pubpropertyLength

Gets or sets the number of elements in the BitArray.

System_CAPS_pubpropertySyncRoot

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

NameDescription
System_CAPS_pubmethodAnd(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.

System_CAPS_pubmethodClone()

Creates a shallow copy of the BitArray.

System_CAPS_pubmethodCopyTo(Array, Int32)

Copies the entire BitArray to a compatible one-dimensional Array, starting at the specified index of the target array.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethodGet(Int32)

Gets the value of the bit at a specific position in the BitArray.

System_CAPS_pubmethodGetEnumerator()

Returns an enumerator that iterates through the BitArray.

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodNot()

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.

System_CAPS_pubmethodOr(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.

System_CAPS_pubmethodSet(Int32, Boolean)

Sets the bit at a specific position in the BitArray to the specified value.

System_CAPS_pubmethodSetAll(Boolean)

Sets all bits in the BitArray to the specified value.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

System_CAPS_pubmethodXor(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.

NameDescription
System_CAPS_pubmethodAsParallel()

Overloaded. Enables parallelization of a query.(Defined by ParallelEnumerable.)

System_CAPS_pubmethodAsQueryable()

Overloaded. Converts an IEnumerable to an IQueryable.(Defined by Queryable.)

System_CAPS_pubmethodCast(Of TResult)()

Casts the elements of an IEnumerable to the specified type.(Defined by Enumerable.)

System_CAPS_pubmethodOfType(Of TResult)()

Filters the elements of an IEnumerable based on a specified type.(Defined by Enumerable.)

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.

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

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1

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.

Return to top
Show: