Using the BitVector Structure in Visual Basic

Introduction

It’s the little things we take for granted. That’s what my mother used to tell me. She was right, especially when applying the same logic to developing a decent program. The little thing I am talking about here is memory. Using memory properly can improve the performance of your application vastly. Today, I will talk about a nifty little structure called BitVector.

Computer Memory

Wikipedia defines computer memory in the following way:

In computing, memory refers to the computer hardware devices used to store information for immediate use in a computer; it is synonymous with the term “primary storage.” Computer memory operates at a high speed, for example random-access memory (RAM), as a distinction from storage that provides slow-to-access program and data storage but offers higher capacities.

Storing Values into Memory

To store information into a memory location, you will most probably have to create a variable. In a computer program, pieces of information get stored into memory by the use of variables. A variable is simply a named location in memory into which you can store any piece of information and then reference it by calling its name. There are, however, many other ways to store pieces of information into memory; these include constants, arrays, and structures.

Data Types

This concept can be broken down further. You can specify the type of information stored into a variable by specifying its datatype.

The .NET Framework allows you to store, manipulate, and pass values between memory. The .NET data types can be broken down into the following subcategories:

  • Integer types
  • Floating-Point types
  • Non-Numeric data types, such as:
    • Boolean
    • Char
    • String
    • Object

Integer Data Types

Integer data types store whole numbers only. The following list demonstrates the integer data types.

Floating-point Types

Three floating types can be used to represent numbers that have a fractional component:

System.Boolean Data Type

The System.Boolean data type represents a value that is either true or false.

System.Char Data Type

The System.Char data type represents a single instance of a 16-bit unicode character.

System.String Data Type

The System.String data type represents a series of character data types. A string can contain a word, a paragraph, or any string of characters.

System.Object Data Type

Every type derives from .System.Object. You can assign any object or value to an object variable.

Arrays

According to MSDN, an array is a set of values that are logically related to each other, such as the number of students in each grade in a grammar school. If you are looking for help on arrays in Visual Basic for Applications (VBA), see the language reference.

By using an array, you can refer to these related values by the same name, and use a number that’s called an index or subscript to tell them apart. The individual values are called the elements of the array. They’re contiguous from index 0 through the highest index value.

Structures

Structures are useful when you want a single variable to hold several related pieces of information. For example, you might want to keep an employee’s name, telephone extension, and salary together.

BitVector

A BitVector structure provides a simple structure that stores Boolean values and small integers in 32 bits of memory.

A BitVector structure can be set up to contain either sections for small integers or bit flags for Booleans, but not both. A BitVector.Section is a window into the BitVector and is composed of the smallest number of consecutive bits that can contain the maximum value specified in CreateSection.

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.

Project

Start a new Visual Basic Console Application and add the following code into the Module that will be shown:

Imports System.Collections.Specialized
Module Module1

   Sub Main()
      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))

      Console.WriteLine(bv.Data)
      Console.WriteLine(bv.Data.ToString("X"))
      Console.ReadLine()
   End Sub

End Module

Conclusion

As you can see, the BitVector structure has many useful properties, and knowing how to make proper use of memory will always improve the performance of your applications. Until next time, cheers!

Hannes DuPreez
Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read