Mosul University

7
  Mosul University Computer science & Mathematics Collage Computer Science Department The report submitted by:  Ahmed F. Fathil Third Class Group    A-

Transcript of Mosul University

Page 1: Mosul University

8/3/2019 Mosul University

http://slidepdf.com/reader/full/mosul-university 1/7

Mosul University

Computer science & Mathematics Collage

Computer Science Department

The report submitted by:

Ahmed F. Fathil

Third Class

Group – A-

Page 2: Mosul University

8/3/2019 Mosul University

http://slidepdf.com/reader/full/mosul-university 2/7

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 datastructure storing a subset of {1,2,...,n} and is effective atexploiting bit-level parallelism in hardware to perform operationsquickly. A typical bit array stores kw bits, where w is the numberof bits in the unit of storage, such as a byte or word, and k is somenonnegative integer. If w does not divide the number of bits to bestored, some space is wasted due to internal fragmentation.

In c# : You have an array of Boolean values or bits and want tostore 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 bitwiseoperations, allowing you to perform bitwise operations, and countand display bits,

which are represented as Booleans, where true indicates that thebit is on (1) and false indicates the bit is off (0).

NameSpace: System.Collections .

Main properties:

The BitArray type exposes the following members.

Name | Description

Count Gets the number of elements contained in theBitArray.

IsReadOnly Gets a value indicating whether the BitArray is read-only.

IsSynchronized Gets a value indicating whether access to the BitArrayis synchronized (thread safe).

Item Gets or sets the value of the bit at a specific position inthe BitArray.

Length Gets or sets the number of elements in the BitArray.

SyncRoot Gets an object that can be used to synchronize accessto the BitArray.

Page 3: Mosul University

8/3/2019 Mosul University

http://slidepdf.com/reader/full/mosul-university 3/7

C# bit array

BitArray Constructor:

Initializes a new instance of the BitArray class whose capacityand initial values can be specified.

This member is overloaded. For complete information about thismember, including syntax, usage, and examples, click a name inthe overload list.

Name | Describe

BitArray(BitArray) Initializes a new instance of the BitArrayclass that contains bit values copied from

the specified BitArray.BitArray(Boolean[]) Initializes a new instance of the BitArrayclass that contains bit values copied fromthe specified array of Booleans.

BitArray(Byte[]) Initializes a new instance of the BitArrayclass that contains bit values copied fromthe specified array of bytes.

BitArray(Int32) Initializes a new instance of the BitArrayclass that can hold the specified number of bit values, which are initially set to false.

BitArray(Int32[]) Initializes a new instance of the BitArrayclass that contains bit values copied from

the specified array of 32-bit integers.

BitArray(Int32,Boolean)

Initializes a new instance of the BitArrayclass that can hold the specified number of bit values, which are initially set to thespecified value.

Page 4: Mosul University

8/3/2019 Mosul University

http://slidepdf.com/reader/full/mosul-university 4/7

C# bit array

Bit array methods :

The BitArray type exposes the following members.

Name | Describe

And Performs the bitwise AND operation on the elements in thecurrent BitArray against the corresponding elements in thespecified BitArray.

Clone Creates a shallow copy of the BitArray.

CopyTo Copies the entire BitArray to a compatible one-dimensionalArray, starting at the specified index of the target array.

Equals(Object) Determines whether the specified Object is equal to thecurrent Object. (Inherited from Object.)

Finalize Allows an object to try to free resources and perform othercleanup operations before it is reclaimed by garbagecollection. (Inherited from Object.)

Get Gets the value of the bit at a specific position in theBitArray.

GetEnumerator Returns an enumerator that iterates through the BitArray.

GetHashCode Serves as a hash function for a particular type. (Inheritedfrom Object.)

GetType Gets the Type of the current instance. (Inherited from

Object.)

MemberwiseClone Creates a shallow copy of the current Object. (Inherited fromObject.)

Not Inverts all the bit values in the current BitArray, so thatelements set to true are changed to false, and elements set tofalse are changed to true.

Or Performs the bitwise OR operation on the elements in thecurrent BitArray against the corresponding elements in thespecified BitArray.

Set Sets the bit at a specific position in the BitArray to thespecified value.

SetAll Sets all bits in the BitArray to the specified value.

ToString Returns a string that represents the current object. (Inheritedfrom Object.)

Xor Performs the bitwise exclusive OR operation on the elementsin the current BitArray against the corresponding elements inthe specified BitArray.

Page 5: Mosul University

8/3/2019 Mosul University

http://slidepdf.com/reader/full/mosul-university 5/7

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

Page 6: Mosul University

8/3/2019 Mosul University

http://slidepdf.com/reader/full/mosul-university 6/7

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();

}

}

Page 7: Mosul University

8/3/2019 Mosul University

http://slidepdf.com/reader/full/mosul-university 7/7

C# bit array

This code produces the following output.

myBA1Count: 5Length: 5Values:False False False False False

myBA2Count: 5Length: 5Values:False False False False False

myBA3Count: 40Length: 40Values:

True False False False False False False FalseFalse True False False False False False False

True True False False False False False FalseFalse False True False False False False False

True False True False False False False FalsemyBA4

Count: 5Length: 5Values:

True False True True FalsemyBA5

Count: 160Length: 160Values:False True True False False False False FalseFalse False False False False False False FalseFalse False False False False False False FalseFalse False False False False False False False

True True True False False False False FalseFalse False False False False False False FalseFalse False False False False False False FalseFalse False False False False False False FalseFalse False False True False False False FalseFalse False False False False False False FalseFalse False False False False False False FalseFalse False False False False False False False

True False False True False False False FalseFalse False False False False False False FalseFalse False False False False False False FalseFalse False False False False False False FalseFalse True False True False False False FalseFalse False False False False False False FalseFalse False False False False False False FalseFalse False False False False False False False