GOA - System.Collections.BitArray

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). This class cannot be inherited.

Definition

public sealed class BitArray: Object, ICollection, IEnumerable, ICloneable

Members Table

MethodDescription
AndPerforms the bitwise AND operation on the elements in the current BitArray against the corresponding elements in the specified BitArray.
BitArrayOverloaded. Initializes a new instance of BitArray.
CloneCreates a shallow copy of the BitArray.
CopyToCopies the entire BitArray to a compatible one-dimensional Array instance at the specified index.
EqualsOverloaded. Compares two objects to determine if they are equal. (Inherited from Object)
GetGets the value of the bit at a specific position in the BitArray.
GetEnumeratorReturns an enumerator that can iterate through the BitArray.
GetHashCodeServes as a hash function for a particular type, suitable for use in hashing algorithms and data structures like a hash table. (Inherited from Object)
GetTypeGets the Type of the current instance. (Inherited from Object)
NotInverts 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.
OrPerforms the bitwise OR operation on the elements in the current BitArray against the corresponding elements in the specified BitArray.
ReferenceEqualsDetermines whether the specified Object instances are the same instance. (Inherited from Object)
SetSets the bit at a specific position in the BitArray to the specified value.
SetAllSets all bits in the BitArray to the specified value.
ToStringReturns a String that represents the current Object. (Inherited from Object)
XorPerforms the bitwise eXclusive OR operation on the elements in the current BitArray against the corresponding elements in the specified BitArray.
this[]Gets or sets the value of the bit at a specific position in the BitArray.
PropertyDescription
CountGets the number of elements contained in the BitArray.
IsReadOnlyGets a value indicating whether the BitArray is read-only.
IsSynchronizedGets a value indicating whether access to the BitArray is synchronized.
LengthGets or sets the number of elements in the BitArray.
SyncRootGets an object that can be used to synchronize access to the BitArray.

Inheritance Hierarchy

Object
BitArray

Examples

// This examples shows how to create and initialize a BitArray.

// It shows also some properties and method of the BitArray class.

 

using System;

using System.Diagnostics;

using System.Collections;

 

public class SampleBitArray

{

     static void Main()

     {

          // Creates and initialize different BitArrays.

          bool[] myBools = new bool[6] {false, true, false, true, false, true};

          BitArray myBA1 = new BitArray(myBools);

 

          BitArray myBA2 = new BitArray( 5, false );

 

          bool[] myBools2 = new bool[5] { true, false, true, true, false };

          BitArray myBA3 = new BitArray( myBools2 );

 

          int[] myInts = new int[3] { 15, 25, 69 };

          BitArray myBA4 = new BitArray( myInts );

 

          bool[] myBools3 = new bool[5] { false, true, true, true, false };

          BitArray myBA5 = new BitArray( myBools3 );

 

          BitArray myBA6 = new BitArray((BitArray)myBA2);

 

          BitArray myBA7 = new BitArray(6);

 

          // Displays the contents of a BitArray.

          Debug.Write("Contents of the BitArray 'myBA1':");

          DisplayValues(myBA1);

 

          Debug.Write("Contents of the BitArray 'myBA4':");

          DisplayValues(myBA4);

 

          Debug.Write("Contents of the BitArray 'myBA6':");

          DisplayValues(myBA6);

 

          Debug.Write("Contents of the BitArray 'myBA7':");

          DisplayValues(myBA7);

 

          // Length property.

          Debug.WriteLine("\nLength of the BitArray 'myBA1':" +myBA1.Length);

 

          // And Method.

          Debug.Write("\nContents of the BitArray 'myBA3':");

          DisplayValues(myBA3);

 

          Debug.Write("\nContents of the BitArray 'myBA5':");

          DisplayValues2(myBA5);

 

          myBA3.And(myBA5);

          Debug.Write("\n\nResult of 'myBA3' AND 'myBA5':");

          DisplayValues(myBA3);

 

          // Clone Method.

          object obj = (object)myBA1.Clone();

          BitArray myBA8 = new BitArray((BitArray)obj);

 

          Debug.Write("\n\nResults after cloning 'myBA1':");

          DisplayValues(myBA8);

 

          // Not Method.

          myBA1.Not();

          Debug.Write("\n\nResults after using the NOT Method on 'myBA1':");

          DisplayValues(myBA1);

 

          // SetAll Method.

          Debug.Write("\n\nContents of the BitArray 'myBA2':");

          DisplayValues(myBA2);

 

          myBA2.SetAll(true);

          Debug.Write("\nResults after using the SetAll 'true' Method on 'myBA2':");

          DisplayValues2(myBA2);

 

          // CopyTo Method.

          Object[] myObjArray = new Object[5] { true, true, true, true, true };

          Debug.Write("\n\nContents of an array of object:");

          DisplayArrayValues(myObjArray);

 

          myBA3.CopyTo(myObjArray,0);

          Debug.Write("\nContents of the array after using the CopyTo Method on 'myBA3':");

          DisplayArrayValues(myObjArray);

     }

 

     // Displays values of a BitArray.

     public static void DisplayValues( BitArray myBitArray )

     {

          int count = myBitArray.Length;

          for(int y=1; y<=count;y++)

          {

               Debug.Write(myBitArray.Get(y-1)+ " ");

          }

     }

 

     // Displays values of a BitArray using an enumerator.

     public static void DisplayValues2( BitArray myBitArray )

     {

          System.Collections.IEnumerator myEnumerator = myBitArray.GetEnumerator();

          while(myEnumerator.MoveNext())

               if (myEnumerator.Current!= null)

                    Debug.Write(myEnumerator.Current +" ");

     }

 

     // Display values of an array.

     public static void DisplayArrayValues (Array myArray)

     {

          for(int y=1; y<=myArray.Length;y++)

          {

               Debug.Write(myArray.GetValue(y-1)+ " ");

          }

     }

}

 

 

/*

 

The output of the sample.

 

Contents of the BitArray "myBA1":False True False True False True

Length of the BitArray "myBA1":6

 

Contents of the BitArray "myBA3":True False True True False

Contents of the BitArray "myBA5":False True True True False

 

Result of "myBA3" AND "myBA5":False False True True False

 

Results after cloning "myBA1":False True False True False True

 

Results after using the NOT Method on "myBA1":True False True False True False

 

Contents of the BitArray "myBA2":False False False False False

Results after using the SetAll "true" Method on "myBA2":True True True True True

 

Contents of an array of object:True True True True True

Contents of the array after using the CopyTo Method on "myBA3":False False True True False

 

*/

Class Information

NamespaceSystem.Collections
Flash Librarycorlib.scl
Flash Library Version2.0.0.2466
Silverlight LibrarySystem.Windows.Forms.dll



© 2003-2007 NETiKA Technologies. All rights reserved.