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 bits and small integers efficiently in a BitVector32

Total Hit ( 3422)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


The System.Collections.Specialized.BitVector32 structure can be used to store up to 32 boolean values, or a set of small integers that can take up to 32 consecutive bits. The BitVector32 is similar to the BitArray class, in that it can hold boolean values that take only one bit each, yet it is more efficient because it is a structure instead of a class.

The simplest way to use a BitVector32 structure is to hold up to 32 boolean values:

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

Dim bv As New BitVector32()
' set one element and read it back
bv(1) = True
Console.WriteLine(bv(1))  ' => True

You can also pass a 32-bit integer to the constructor to initialize all the elements in one pass. For example:

Click here to copy the following block
'initialize all elements to True
bv = new BitVector32(-1)

To define a BitVector32 that is subdivided in sections that are longer than 1 bit you must create one or more BitVector32.Section objects, and use them when you later read and write individual elements. You define a section by means of the BitVector32.CreateSection shared method, that takes the highest integer you want to store in that section and (for all sections after the 1st one) the previous section. Here's a complete example:

Click here to copy the following block
Dim bv As New BitVector32()

' create three sections, of 4, 5, and 6 bits each
Dim se1 As BitVector32.Section = BitVector32.CreateSection(15)
Dim se2 As BitVector32.Section = BitVector32.CreateSection(31, se1)
Dim se3 As BitVector32.Section = BitVector32.CreateSection(63, se2)

' set each section at a given value
bv(se1) = 10
bv(se2) = 20
bv(se3) = 40
' read them back
Console.WriteLine(bv(se1))
Console.WriteLine(bv(se2))
Console.WriteLine(bv(se3))

The Data property sets or returns the internal 32-bit value; you can use this property to save the value into a database field:

Click here to copy the following block
' continuing the previous code sample
' read the entire field as a 32-bit value
Console.WriteLine(bv.Data)         ' => -11958
Console.WriteLine(bv.Data.ToString("X"))  ' => FFFFD14A


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.