.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> .Net Framework >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Understanding the BitArray Collection

Posted By:Hefin Dsouza       Posted Date: September 04, 2009    Points: 25    Category: .Net Framework    URL: http://www.dotnetspark.com  
 

In this article I will be Explaining the use of BitArray Collection



The BitArray Collection Type


BitArray :- The BitArray collections can only store Boolean Values and cannot dynamically increase its size so in the constructor of the BitArray we need to specify the Capacity.The size can be increased by using the Length property.The BitArray Class is in the System.Collections namespace.
Example

BitArray com = new BitArray(5);
com[0] = true;
com[1] = false;
com[2] = true;

In the Above code The Bit Array is created with the size of 5 and only 3 have been initialised the rest 2 will be false by default.
To display the output

foreach (bool item in com)
{
Console.WriteLine(item);
}

The output will be

True
False
True
False
False


Apart from only storing Boolean values in the BitArray class it also supports common Bit-level operations such as OR,AND ,Xor(eXclusive OR) and Not.
Example : To demonstrate the use of BitLevel operations
OR

BitArray com = new BitArray(4);
com[0] = true;
com[1] = false;
com[2] = true;
com[3] = true;

BitArray com1 = new BitArray(4);
com1[0] = false;
com1[1] = true;
com1[2] = false;
com1[3] = true;
BitArray orComCom1 = com.Or(com1);

Console.WriteLine(" COM\tCOM1\tOR");
Console.WriteLine("------------------------------------");
for (int i = 0; i < com.Length; i++)
{
Console.WriteLine("{0}\t{1}\t{2}",com[i],com1[i],orComCom1[i]);
}

Similarly you can try it with the Other Bit level Operators.

P.S.: The BitArray Class does not have the Add() or Remove() method because each value in the BitArray can either be True or False.

Regards
Hefin Dsouza

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend