BitArray

BitArray


Manages a compact array of bit values. Each bit represents a boolean, where True = 1, and False = 0.


Implements:

ICloneable 
ICollection 
IEnumerable 
IObject 
IVersionable 

Public:

Properties:

NameDescription
 Count (get) Gets the number of bits being represented in the array.  
 IsReadOnly (get) Gets if the instance is a read-only instance.  
 Item (get) Gets the value of a specific bit in the array.  
 Item (let) Sets the value of a specific bit in the array.  
 Length (get) Gets the number of bits represented in the array.  
 Length (let) Sets the number of bits represented in the array.  

Methods:

NameDescription
 AndBits Performs a bitwise AND on the current instance of BitArray using the bits in another instance of BitArray.  
 Clone Returns a duplicate of the current instance.  
 CopyTo Copies the internal bit-array to a compatible array.  
 Equals Returns a boolean indicating if the value and this object instance are the same instance.  
 GetEnumerator Returns an enumerator for this instance.  
 GetHashCode Returns a pseudo-unique number identifying this instance.  
 NotBits Performs bitwise negate operation on the internal array.  
 OrBits Performs a bitwise OR on the current instance of BitArray using the bits in another instance of BitArray.  
 SetAll Sets all of the bits to the specified value of True (1) or False (0).  
 ToString Returns a string representation of this object instance.  
 XorBits Performs a bitwise XOR on the current instance of BitArray using the bits in another instance of BitArray.  

Remarks

The default BitArray size is 32 bits unless a constructor is used to define a custom size.

Examples

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

Private Sub Main()
    Const CountFormatter As String = "   Count:    {0}"
    Const LengthFormatter As String = "   Length:   {0}"
    Dim MyBA1 As BitArray
    Dim MyBA2 As BitArray
    Dim MyBA3 As BitArray
    Dim MyBA4 As BitArray
    Dim MyBA5 As BitArray
    
    ' Creates and initializes several BitArrays.
    Set MyBA1 = NewBitArray(5)
    Set MyBA2 = NewBitArray(5, True)
    Set MyBA3 = BitArray.FromBytes(NewBytes(1, 2, 3, 4, 5))
    Set MyBA4 = BitArray.FromBooleans(NewBooleans(True, False, True, True, False))
    Set MyBA5 = BitArray.FromLongs(NewLongs(6, 7, 8, 9, 10))
    
    ' Displays the properties and values of the BitArrays.
    Debug.Print "MyBA1"
    Debug.Print CorString.Format(CountFormatter, MyBA1.Count)
    Debug.Print CorString.Format(CountFormatter, MyBA1.Length)
    Debug.Print "   Values:"
    PrintValues MyBA1, 8
    
    Debug.Print "MyBA2"
    Debug.Print CorString.Format(CountFormatter, MyBA2.Count)
    Debug.Print CorString.Format(CountFormatter, MyBA2.Length)
    Debug.Print "   Values:"
    PrintValues MyBA2, 8
    
    Debug.Print "MyBA3"
    Debug.Print CorString.Format(CountFormatter, MyBA3.Count)
    Debug.Print CorString.Format(CountFormatter, MyBA3.Length)
    Debug.Print "   Values:"
    PrintValues MyBA3, 8

    Debug.Print "MyBA4"
    Debug.Print CorString.Format(CountFormatter, MyBA4.Count)
    Debug.Print CorString.Format(CountFormatter, MyBA4.Length)
    Debug.Print "   Values:"
    PrintValues MyBA4, 8

    Debug.Print "MyBA5"
    Debug.Print CorString.Format(CountFormatter, MyBA5.Count)
    Debug.Print CorString.Format(CountFormatter, MyBA5.Length)
    Debug.Print "   Values:"
    PrintValues MyBA5, 8
End Sub

Private Sub PrintValues(ByVal MyList As IEnumerable, ByVal MyWidth As Long)
    Dim i As Long
    Dim Value As Variant
    
    i = MyWidth
    
    For Each Value In MyList
        If i <= 0 Then
            i = MyWidth
            Debug.Print
        End If
        
        i = i - 1
        Debug.Print CorString.Format("{0,8}", Value);
    Next
    
    Debug.Print
End Sub


' This code produces the following output.
'
'    MyBA1
'       Count:    5
'       Count:    5
'    Values:
'       False   False   False   False   False
'    MyBA2
'       Count:    5
'       Count:    5
'    Values:
'       True    True    True    True    True
'    MyBA3
'       Count:    40
'       Count:    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
'       Count:    5
'    Values:
'        True   False    True    True   False
'    MyBA5
'       Count:    160
'       Count:    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

See Also

Project CorLib Overview

Class BitArray Overview

Constructors

BitArrayStatic