Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Store large Boolean arrays in a BitArray object

Total Hit ( 5852)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


.NET Boolean values require 4 bytes each, as opposed to the 2 byte taken under previous VB versions. When creating very large Boolean arrays this extra memory impacts negatively on the application's performance. The .NET Framework offers a specialized class, the System.Collection.BitArray class, which lets you store a number of boolean values as if it were a regular array, yet it takes only 1 bit for each element.

You pass the capacity of the array - that is, the number of Boolean values you want to store - to the BitArray's constructor, and then you write and read its elements as if it were a regular array:

Click here to copy the following block
' This code assumes that you have the following Imports
'  Imports System.Collections

' an array of 1024 boolean values
Dim ba As New BitArray(1024)
' set the first element
ba(0) = True
' read it back
Console.WriteLine(ba(0))

All the elements in a freshly created BitArray are initialized to False, but you can set them to True by using the following constructor

Click here to copy the following block
Dim ba As New BitArray(1024, True)

or with the SetAll method

Click here to copy the following block
ba.SetAll(True)   ' or False

You can also pass a true Boolean array to the BitArray constructor, so that each element in the BitArray is initialized with the corresponding element in the original Boolean array.
Finally, you can perform bitwise operations on all the members of a BitArray by means of Not, And, Or, and Xor methods. All these methods (except Not) take another BitArray as an argument, so that all the elements of the current BitArray are combined with the corresponding element of the other BitArray according to the specified operation:

Click here to copy the following block
Dim ba1 As New BitArray(1024)
Dim ba2 As New BitArray(1024)
' initialize both BitArrays
' ...
' AND all the elements in ba1 with the elements in ba2
ba1.And(ba2)


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.