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

Inheritance Hierarchy

System.Object
  System.Collections.BitArray

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

Syntax

'Declaration
<ComVisibleAttribute(True)> _
<DefaultMemberAttribute("Item")> _
Public NotInheritable Class BitArray _
    Implements ICollection, IEnumerable
[ComVisibleAttribute(true)]
[DefaultMemberAttribute("Item")]
public sealed class BitArray : ICollection, 
    IEnumerable

The BitArray type exposes the following members.

Constructors

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 BitArray(BitArray) Initializes a new instance of the BitArray class that contains bit values copied from the specified BitArray.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 BitArray(array<Boolean[]) Initializes a new instance of the BitArray class that contains bit values copied from the specified array of Booleans.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 BitArray(array<Byte[]) Initializes a new instance of the BitArray class that contains bit values copied from the specified array of bytes.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 BitArray(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 Silverlight for Windows PhoneSupported by Xbox 360 BitArray(array<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 Silverlight for Windows PhoneSupported by Xbox 360 BitArray(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

Properties

  Name Description
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Count Gets the number of elements contained in the BitArray.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IsReadOnly Gets a value indicating whether the BitArray is read-only.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IsSynchronized Gets a value indicating whether access to the BitArray is synchronized (thread safe).
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Item Gets or sets the value of the bit at a specific position in the BitArray.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Length Gets or sets the number of elements in the BitArray.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 SyncRoot Gets an object that can be used to synchronize access to the BitArray.

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 And Performs the bitwise AND operation on the elements in the current BitArray against the corresponding elements in the specified BitArray.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Clone Creates a shallow copy of the BitArray.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 CopyTo Copies the entire BitArray to a compatible one-dimensional Array, starting at the specified index of the target array.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Get Gets the value of the bit at a specific position in the BitArray.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetEnumerator Returns an enumerator that iterates through the BitArray.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetType Gets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Not 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.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Or Performs the bitwise OR operation on the elements in the current BitArray against the corresponding elements in the specified BitArray.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Set Sets the bit at a specific position in the BitArray to the specified value.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 SetAll Sets all bits in the BitArray to the specified value.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString Returns a string that represents the current object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Xor Performs the bitwise exclusive OR operation on the elements in the current BitArray against the corresponding elements in the specified BitArray.

Top

Extension Methods

  Name Description
Public Extension MethodSupported by Silverlight for Windows Phone AsQueryable Converts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension MethodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Cast<TResult> Converts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension MethodSupported by Silverlight for Windows PhoneSupported by Xbox 360 OfType<TResult> Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)

Top

Remarks

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.

Examples

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

Imports System.Collections

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' 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.
      outputBlock.Text &= "myBA1" & vbCrLf
      outputBlock.Text += String.Format("   Count:    {0}", myBA1.Count) & vbCrLf
      outputBlock.Text += String.Format("   Length:   {0}", myBA1.Length) & vbCrLf
      outputBlock.Text &= "   Values:" & vbCrLf
      PrintValues(outputBlock, myBA1, 8)

      outputBlock.Text &= "myBA2" & vbCrLf
      outputBlock.Text += String.Format("   Count:    {0}", myBA2.Count) & vbCrLf
      outputBlock.Text += String.Format("   Length:   {0}", myBA2.Length) & vbCrLf
      outputBlock.Text &= "   Values:" & vbCrLf
      PrintValues(outputBlock, myBA2, 8)

      outputBlock.Text &= "myBA3" & vbCrLf
      outputBlock.Text += String.Format("   Count:    {0}", myBA3.Count) & vbCrLf
      outputBlock.Text += String.Format("   Length:   {0}", myBA3.Length) & vbCrLf
      outputBlock.Text &= "   Values:" & vbCrLf
      PrintValues(outputBlock, myBA3, 8)

      outputBlock.Text &= "myBA4" & vbCrLf
      outputBlock.Text += String.Format("   Count:    {0}", myBA4.Count) & vbCrLf
      outputBlock.Text += String.Format("   Length:   {0}", myBA4.Length) & vbCrLf
      outputBlock.Text &= "   Values:" & vbCrLf
      PrintValues(outputBlock, myBA4, 8)

      outputBlock.Text &= "myBA5" & vbCrLf
      outputBlock.Text += String.Format("   Count:    {0}", myBA5.Count) & vbCrLf
      outputBlock.Text += String.Format("   Length:   {0}", myBA5.Length) & vbCrLf
      outputBlock.Text &= "   Values:" & vbCrLf
      PrintValues(outputBlock, myBA5, 8)

   End Sub 'Main

   Public Shared Sub PrintValues(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal myList As IEnumerable, ByVal myWidth As Integer)
      Dim i As Integer = myWidth
      Dim obj As [Object]
      For Each obj In myList
         If i <= 0 Then
            i = myWidth
            outputBlock.Text &= vbCrLf
         End If
         i -= 1
         outputBlock.Text += String.Format("{0,8}", obj)
      Next obj
      outputBlock.Text &= vbCrLf
   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

using System;
using System.Collections;
public class Example
{

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {

      // 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.
      outputBlock.Text += "myBA1" + "\n";
      outputBlock.Text += String.Format("   Count:    {0}", myBA1.Count) + "\n";
      outputBlock.Text += String.Format("   Length:   {0}", myBA1.Length) + "\n";
      outputBlock.Text += "   Values:" + "\n";
      PrintValues(outputBlock, myBA1, 8);

      outputBlock.Text += "myBA2" + "\n";
      outputBlock.Text += String.Format("   Count:    {0}", myBA2.Count) + "\n";
      outputBlock.Text += String.Format("   Length:   {0}", myBA2.Length) + "\n";
      outputBlock.Text += "   Values:" + "\n";
      PrintValues(outputBlock, myBA2, 8);

      outputBlock.Text += "myBA3" + "\n";
      outputBlock.Text += String.Format("   Count:    {0}", myBA3.Count) + "\n";
      outputBlock.Text += String.Format("   Length:   {0}", myBA3.Length) + "\n";
      outputBlock.Text += "   Values:" + "\n";
      PrintValues(outputBlock, myBA3, 8);

      outputBlock.Text += "myBA4" + "\n";
      outputBlock.Text += String.Format("   Count:    {0}", myBA4.Count) + "\n";
      outputBlock.Text += String.Format("   Length:   {0}", myBA4.Length) + "\n";
      outputBlock.Text += "   Values:" + "\n";
      PrintValues(outputBlock, myBA4, 8);

      outputBlock.Text += "myBA5" + "\n";
      outputBlock.Text += String.Format("   Count:    {0}", myBA5.Count) + "\n";
      outputBlock.Text += String.Format("   Length:   {0}", myBA5.Length) + "\n";
      outputBlock.Text += "   Values:" + "\n";
      PrintValues(outputBlock, myBA5, 8);
   }

   public static void PrintValues(System.Windows.Controls.TextBlock outputBlock, IEnumerable myList, int myWidth)
   {
      int i = myWidth;
      foreach (Object obj in myList)
      {
         if (i <= 0)
         {
            i = myWidth;
            outputBlock.Text += "\n";
         }
         i--;
         outputBlock.Text += String.Format("{0,8}", obj);
      }
      outputBlock.Text += "\n";
   }

}


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

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

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.