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

Top 5 Contributors of the Month

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.

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)

The output will be


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

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");
for (int i = 0; i < com.Length; 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.

Hefin Dsouza

 Subscribe to Articles


Further Readings:


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