BitArray Class
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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).
public ref class BitArray sealed : System::Collections::ICollection
public ref class BitArray sealed : ICloneable, System::Collections::ICollection
public sealed class BitArray : System.Collections.ICollection
public sealed class BitArray : ICloneable, System.Collections.ICollection
[System.Serializable]
public sealed class BitArray : ICloneable, System.Collections.ICollection
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class BitArray : ICloneable, System.Collections.ICollection
type BitArray = class
interface ICollection
interface IEnumerable
type BitArray = class
interface ICollection
interface IEnumerable
interface ICloneable
[<System.Serializable>]
type BitArray = class
interface ICollection
interface IEnumerable
interface ICloneable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type BitArray = class
interface ICollection
interface IEnumerable
interface ICloneable
Public NotInheritable Class BitArray
Implements ICollection
Public NotInheritable Class BitArray
Implements ICloneable, ICollection
- Inheritance
-
BitArray
- Attributes
- Implements
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
*/
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
*/
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
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
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
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.
Bit |
Initializes a new instance of the BitArray class that contains bit values copied from the specified BitArray. |
Bit |
Initializes a new instance of the BitArray class that contains bit values copied from the specified array of Booleans. |
Bit |
Initializes a new instance of the BitArray class that contains bit values copied from the specified array of bytes. |
Bit |
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. |
Bit |
Initializes a new instance of the BitArray class that can hold the specified number of bit values, which are initially set to |
Bit |
Initializes a new instance of the BitArray class that contains bit values copied from the specified array of 32-bit integers. |
Count |
Gets the number of elements contained in the BitArray. |
Is |
Gets a value indicating whether the BitArray is read-only. |
Is |
Gets a value indicating whether access to the BitArray is synchronized (thread safe). |
Item[Int32] |
Gets or sets the value of the bit at a specific position in the BitArray. |
Length |
Gets or sets the number of elements in the BitArray. |
Sync |
Gets an object that can be used to synchronize access to the BitArray. |
And(Bit |
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. |
Clone() |
Creates a shallow copy of the BitArray. |
Copy |
Copies the entire BitArray to a compatible one-dimensional Array, starting at the specified index of the target array. |
Equals(Object) |
Determines whether the specified object is equal to the current object. (Inherited from Object) |
Get(Int32) |
Gets the value of the bit at a specific position in the BitArray. |
Get |
Returns an enumerator that iterates through the BitArray. |
Get |
Serves as the default hash function. (Inherited from Object) |
Get |
Gets the Type of the current instance. (Inherited from Object) |
Has |
Determines whether all bits in the BitArray are set to |
Has |
Determines whether any bit in the BitArray is set to |
Left |
Shifts all the bit values of the current BitArray to the left on |
Memberwise |
Creates a shallow copy of the current Object. (Inherited from Object) |
Not() |
Inverts all the bit values in the current BitArray, so that elements set to |
Or(Bit |
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. |
Right |
Shifts all the bit values of the current BitArray to the right on |
Set(Int32, Boolean) |
Sets the bit at a specific position in the BitArray to the specified value. |
Set |
Sets all bits in the BitArray to the specified value. |
To |
Returns a string that represents the current object. (Inherited from Object) |
Xor(Bit |
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. |
ICollection. |
Copies the elements of the BitArray to an Array, starting at the specified Array index. |
ICollection. |
Gets the number of elements in the BitArray. |
ICollection. |
Gets a value that indicates whether access to the BitArray is synchronized (thread safe). |
ICollection. |
Gets an object that can be used to synchronize access to the BitArray. |
Cast<TResult>(IEnumerable) |
Casts the elements of an IEnumerable to the specified type. |
Of |
Filters the elements of an IEnumerable based on a specified type. |
As |
Enables parallelization of a query. |
As |
Converts an IEnumerable to an IQueryable. |
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |
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.
.NET feedback
.NET is an open source project. Select a link to provide feedback: