Professional Documents
Culture Documents
C#
bit array
is an array data structure that compactly stores individual bits (Boolean values). It implements a simple set data structure storing a subset of {1,2,...,n} and is effective at exploiting bit-level parallelism in hardware to perform operations quickly. A typical bit array stores kw bits, where w is the number of bits in the unit of storage, such as a byte or word, and k is some nonnegative integer. If w does not divide the number of bits to be stored, some space is wasted due to internal fragmentation.
| Description
Gets the number of elements contained in the BitArray. Gets a value indicating whether the BitArray is readonly. Gets a value indicating whether access to the BitArray is synchronized (thread safe). Gets or sets the value of the bit at a specific position in the BitArray. Gets or sets the number of elements in the BitArray. Gets an object that can be used to synchronize access to the BitArray.
Length SyncRoot
C#
bit array
BitArray Constructor: Initializes a new instance of the BitArray class whose capacity and initial values can be specified. This member is overloaded. For complete information about this member, including syntax, usage, and examples, click a name in the overload list. Name BitArray(BitArray) | Describe Initializes a new instance of the BitArray class that contains bit values copied from the specified BitArray. Initializes a new instance of the BitArray class that contains bit values copied from the specified array of Booleans.
BitArray(Boolean[])
BitArray(Byte[])
Initializes a new instance of the BitArray class that contains bit values copied from the specified array of bytes.
Initializes a new instance of the BitArray class that can hold the specified number of bit values, which are initially set to false. Initializes a new instance of the BitArray class that contains bit values copied from the specified array of 32-bit integers. 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.
BitArray(Int32)
BitArray(Int32[])
BitArray(Int32, Boolean)
C#
bit array
Performs the bitwise AND operation on the elements in the current BitArray against the corresponding elements in the specified BitArray. Creates a shallow copy of the BitArray. Copies the entire BitArray to a compatible one-dimensional Array, starting at the specified index of the target array. Determines whether the specified Object is equal to the current Object. (Inherited from Object.) Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) Gets the value of the bit at a specific position in the BitArray. Returns an enumerator that iterates through the BitArray.
C#
bit array
Code Examples
Creating BitArrays BitArray myBitArray = new BitArray( 5 ); //Creates a BitArray of length 5 with all values set to false.
BitArray myBitArray = new BitArray( 5, false ); //Creates a BitArray of length 5 with all values set to true.
BitArray myBitArray = new BitArray( 5, true ); //Creates a BitArray of length 5 with all values set to true.
byte[] myBytes = new byte[5] { 1, 2, 3, 4, 5 }; BitArray myBitArray = new BitArray( myBytes ); //Creates a BitArray of length 40 with bit pattern equal to the binary //equivalent of each number in the byte array (8 bits per byte).
bool[] myBools = new bool[5] { true, false, true, true, false }; BitArray myBitArray = new BitArray( myBools ); //Creates a BitArray of length 5 with bit pattern equal to the bool //array.
int[] myInts = new int[5] { 6, 7, 8, 9, 10 }; BitArray myBitArray = new BitArray( myInts ); //Creates a BitArray of length 160 with bit pattern equal to the binary //equivalent of each number in the int array (32 bits per int).
C#
bit array
Completed code show how to create and initialize a BitArray and how to print out its values.
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(); } } {
bit array
False False False False False False False False False False False False False False False False False False False False