You are on page 1of 7

Mosul University Computer science & Mathematics Collage Computer Science Department

The report submitted by: Ahmed F. Fathil Third Class Group A-

C#

bit array

A bit array (also known as a bitmap, a bitset, or a bitstring):

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.

In c# : You have an array of Boolean values or bits and want to


store them in a compact and easy-to-use object in the C# programming language. The BitArray class in System. Collections offers an ideal and clear interface to bitwise operations, allowing you to perform bitwise operations, and count and display bits, which are represented as Booleans, where true indicates that the bit is on (1) and false indicates the bit is off (0). NameSpace: System.Collections. Main properties:
The BitArray type exposes the following members.

Name Count IsReadOnly IsSynchronized Item

| 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

Bit array methods :


The BitArray type exposes the following members. Name And Clone CopyTo Equals(Object) Finalize Get GetEnumerator GetHashCode GetType MemberwiseClone Not Or Set SetAll ToString Xor | Describe

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.

Serves as a hash function for a particular type. (Inherited from Object.)


Gets the Type of the current instance. (Inherited from Object.) Creates a shallow copy of the current Object. (Inherited from Object.) 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. Performs the bitwise OR operation on the elements in the current BitArray against the corresponding elements in the specified BitArray. Sets the bit at a specific position in the BitArray to the specified value. Sets all bits in the BitArray to the specified value. Returns a string that represents the current object. (Inherited from Object.) Performs the bitwise exclusive OR operation on the elements in the current BitArray against the corresponding elements in the specified 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(); } } {

C# This code produces the following output.


myBA1 Count: Length: Values: False myBA2 Count: Length: Values: False myBA3 Count: Length: Values: True False True False True myBA4 Count: Length: Values: True myBA5 Count: Length: Values: False False False False True False False False False False False False True False False False False False False False 5 5 False 5 5 False 40 40 False True True False False 5 5 False 160 160 True False False False True False False False False False False False False False False False True False False False True 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 True False False False True 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 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 False False False True True False False False 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

bit array

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

You might also like