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.
Wikipedia defines computer memory in the following way:
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.
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:
Integer Data Types
Integer data types store whole numbers only. The following list demonstrates the integer data 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.
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 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.
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.
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
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!