Store bits and small integers efficiently in a BitVector32

Store bits and small integers efficiently in a BitVector32

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:

' This code assume that you have the following Imports'    Imports System.Collections.SpecializedDim bv As New BitVector32()' set one element and read it backbv(1) = TrueConsole.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:

'initialize all elements to Truebv = 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:

Dim bv As New BitVector32()' create three sections, of 4, 5, and 6 bits eachDim 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 valuebv(se1) = 10bv(se2) = 20bv(se3) = 40' read them backConsole.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:

' continuing the previous code sample' read the entire field as a 32-bit valueConsole.WriteLine(bv.Data)                  ' => -11958Console.WriteLine(bv.Data.ToString("X"))    ' => FFFFD14A

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes