Trainers' musings on .NET, Silverlight, and SharePoint

In many cases, if you have not done an extensive review of the .NET Framework, you probably haven’t discovered some rather interesting features that exist.  One such feature is the BitVector32 structure.  I would suspect that only those of us who have spent any time preparing for taking a certification exam are familiar with BitVector32.

The BitVector32 structure is in the System.Collections.Specialized namespace.  Essentially, it is a single array (vector) of 32 bits, where a value of one corresponds to true (on) and a value of zero is false (off).  By default, when creating a new BitVector32 object, you will start with all of the bits turned off.  As an alternative, you can pass a parameter into the constructor to turn on some of the bits.  If you want all of the bits turned on, pass the value of -1, which is the bitwise complement to zero.  This is demonstrated in the following code:

BitVector32 bvOff = new BitVector32();
BitVector32 bvOn = new BitVector32(-1);
BitVector32 bvDemo15 = new BitVector32(15);
Console.WriteLine("Output:");
Console.WriteLine("bvOff    = {0}", bvOff.ToString());
Console.WriteLine("bvOn     = {0}", bvOn.ToString());
Console.WriteLine("bvDemo15 = {0}", bvDemo15.ToString());
//Output:
//bvOff    = BitVector32{00000000000000000000000000000000}
//bvOn     = BitVector32{11111111111111111111111111111111}
//bvDemo15 = BitVector32{00000000000000000000000000001111}

The BitVector32 structure is powered by two methods: CreateMask and CreateSection.

The CreateMask method is a static method that allows for the creation of a series of masks that turns the BitVector32 object into a set of bit flags.  We see this in the following example:

BitVector32 bvDemo = new BitVector32();

int[] aintMasks = new int[5];

for (int i = 0; i < 5; i++)
{
	if (i == 0)
		aintMasks[i] = BitVector32.CreateMask();
	else
		aintMasks[i] = BitVector32.CreateMask(aintMasks[i - 1]);
}

Console.WriteLine("Output after setting each mask to true:");

foreach (int intMask in aintMasks)
{
	bvDemo[intMask] = true;

	Console.WriteLine("intMask = {0}\tbvDemo = {1}", intMask, bvDemo.ToString());

	bvDemo[intMask] = false;
}

//Output after setting each mask to true:
//intMask = 1     bvDemo = BitVector32{00000000000000000000000000000001}
//intMask = 2     bvDemo = BitVector32{00000000000000000000000000000010}
//intMask = 4     bvDemo = BitVector32{00000000000000000000000000000100}
//intMask = 8     bvDemo = BitVector32{00000000000000000000000000001000}
//intMask = 16    bvDemo = BitVector32{00000000000000000000000000010000}

As you can see from the results, CreateMask without a parameter sets the first mask to the first bit.  By using the previous bit flag as the parameter, we set the mask on the next bit value within the BitVector32 object.  Of course, we can extend this so that we have masks on all 32 bits.

With the CreateSection method, we can parse the BitVector32 object into a series of sections based on a set of short integers.  In the following example, we create four sections based on the values of 1, 3, 7, and 15 respectively.

BitVector32 bvDemo = new BitVector32();

BitVector32.Section bvsSection1 = BitVector32.CreateSection(1);
BitVector32.Section bvsSection2 = BitVector32.CreateSection(3, bvsSection1);
BitVector32.Section bvsSection3 = BitVector32.CreateSection(7, bvsSection2);
BitVector32.Section bvsSection4 = BitVector32.CreateSection(15, bvsSection3);

Console.WriteLine("Initial section values:");
Console.WriteLine("\tSection1: {0}", bvDemo[bvsSection1]);
Console.WriteLine("\tSection2: {0}", bvDemo[bvsSection2]);
Console.WriteLine("\tSection3: {0}", bvDemo[bvsSection3]);
Console.WriteLine("\tSection4: {0}", bvDemo[bvsSection4]);

Console.WriteLine();
Console.WriteLine("BitVector32 output after setting each section to different values:");
Console.WriteLine("\tInitial:         {0}", bvDemo.ToString());

bvDemo[bvsSection1] = 1;
Console.WriteLine("\tSection1 =  1:   {0}", bvDemo.ToString());

bvDemo[bvsSection2] = 2;
Console.WriteLine("\tSection2 =  2:   {0}", bvDemo.ToString());

bvDemo[bvsSection3] = 5;
Console.WriteLine("\tSection3 =  5:   {0}", bvDemo.ToString());

bvDemo[bvsSection4] = 11;
Console.WriteLine("\tSection5 = 11:   {0}", bvDemo.ToString());

Console.WriteLine();
Console.WriteLine("New section values:");
Console.WriteLine("\tSection1: {0}", bvDemo[bvsSection1]);
Console.WriteLine("\tSection2: {0}", bvDemo[bvsSection2]);
Console.WriteLine("\tSection3: {0}", bvDemo[bvsSection3]);
Console.WriteLine("\tSection4: {0}", bvDemo[bvsSection4]);

//Initial section values:
//        Section1: 0
//        Section2: 0
//        Section3: 0
//        Section4: 0

//BitVector32 output after setting each section to different values:
//        Initial:         BitVector32{00000000000000000000000000000000}
//        Section1 =  1:   BitVector32{00000000000000000000000000000001}
//        Section2 =  2:   BitVector32{00000000000000000000000000000101}
//        Section3 =  5:   BitVector32{00000000000000000000000000101101}
//        Section5 = 11:   BitVector32{00000000000000000000001011101101}

//New section values:
//        Section1: 1
//        Section2: 2
//        Section3: 5
//        Section4: 11

A practical usage for the BitVector32 sections would be associating each section to a multiple-choice question for a test or a survey.  For example, when using four possible choices for each question, each section could be set to a value of 3 which would allow for the values of 0 (00), 1 (01), 2 (10), or 3 (11).  Thus, you could create a test or survey that had up to 16 questions.

The BitVector32 structure provides .NET developers with another choice in how to gather and process bit data.  In addition to some of its practical uses, it is a very interesting component within the .NET Framework. To learn more about the .NET Framework features, consider our .NET training.

No TweetBacks yet. (Be the first to Tweet this post)

Leave a Reply

You must be logged in to post a comment.

© Webucator, Inc. All rights reserved. | Toll Free: 877-932-8228 | UK: 0808-101-3484 | From outside the USA: 315-849-2724 | Fax: 315-849-2723