Over a million developers have joined DZone.

BitVector32 in .NET

DZone's Guide to

BitVector32 in .NET

Free Resource
After talking about BitArray class in .NET as a lesser-used data collection type designed for improving the efficiency and ease of dealing with bits and small integers, I think it’s also worth talking about a related structure, BitVector32, located in System.Collection.Specialized namespace. As the namespace suggests, this is also a specialized collection type that is built with the intention to improve the efficiency of operating on bits and small integers.

Bit Vector is a known data structure in computer programming for specific applications and in one way or another each programming framework provides a good way to deal with this structure. Essentially, bit vectors provide a mechanism for dealing with bits and small integers in a space-efficient manner.

It’s important to emphasize on the fact that unlike BitArray that is a class, BitVector32 is a structure and is built based on another structure, System.Collections.Specialized.Bitvector32.Section. BitVector32 has a better efficiency than BitArray as the latter one can grow as much as needed (as I described before) but the former one has a constant 32 bits storage.

A BitVector32 can be set up in two different ways: using sections for small integers or bit flags for Booleans. Generally, using the section to work with a BitVector32 requires special attention as a tiny mistake can yield strange results.

BitVector32 provides two constructors:

  • BitVector32(BitVector32): Initiates the structure using the data in another BitVector32 object.
  • BitVector32(Int32): Initiates the structure using the data represented by an integer number.

It also provides two properties:

  • Data: The value represented by the structure as an integer.
  • Item: The value stored in a specific section or mask.

Unlike BitArray, BitVector32 provides two methods (with their overloads) only:

  • CreateMask: Creates the first mask in a series.
  • CreateSection: Creates additional masks in a series.

Note that BitVector32 is designed specifically for 32 bit integers and there is no available structure for other lengths of integers in the .NET Framework, however, Rob Kennedy has implemented his own BitVector64 structure that you can use if you need to have such a structure in your code.

Bit Flags

One way to use BitVector32 structure is to apply bit flags where you can apply CreateMask methods to create a series of masks and set specific items to the bit flag that you desire.

In the below example, I create a simple BitVector32 object using a series of masks to build an integer.

private static void BitFlags()
03. // Initialize the vector to 2: 00000000 00000000 00000000 00000010
04. BitVector32 bitVector = new BitVector32(2);
06. Console.WriteLine("Initially, the value of vector is {0} represented as \n {1}",
07. bitVector.Data, bitVector.ToString());
09. int bit1 = BitVector32.CreateMask();
10. int bit2 = BitVector32.CreateMask(bit1);
11. int bit3 = BitVector32.CreateMask(bit2);
13. // Set the first bit to true: 00000000 00000000 00000000 00000011
14. bitVector[bit1] = true;
15. Console.WriteLine("Setting the first bit to true, the value of vector is {0} represented as \n {1}",
16. bitVector.Data, bitVector.ToString());
18. // Set the second bit to false: 00000000 00000000 00000000 00000001
19. bitVector[bit2] = false;
20. Console.WriteLine("Setting the second bit to false, the value of vector is {0} represented as \n {1}",
21. bitVector.Data, bitVector.ToString());
23. // Set the third bit to true: 00000000 00000000 00000000 00000101
24. bitVector[bit3] = true;
25. Console.WriteLine("Setting the third bit to true, the value of vector is {0} represented as \n {1}",
26. bitVector.Data, bitVector.ToString());


Here I initialize my BitVector32 structure to the integer value 2 then create three masks in a sequence and set different bits to customize the structure and get different results.



The other way to work with BitVector32 structures is to use BitVector32.Section structure. In this approach you build the vector from a set of smaller section from right to left with different values and sizes.

private static void Sections()
03. // Initialize the vector to 0: 00000000 00000000 00000000 00000000
04. BitVector32 bitVector = new BitVector32(0);
06. // Initialize the first section with 2 bits: 00
07. BitVector32.Section section1 = BitVector32.CreateSection(3);
08. Console.WriteLine("Initial value of section 1: {0}", bitVector[section1]);
10. // Initialize the second section with 3 bits: 000
11. BitVector32.Section section2 = BitVector32.CreateSection(7, section1);
12. Console.WriteLine("Initial value of section 2: {0}", bitVector[section2]);
14. // Initialize the third section with 4 bits: 0000
15. BitVector32.Section section3 = BitVector32.CreateSection(11, section2);
16. Console.WriteLine("Initial value of section 3: {0}", bitVector[section3]);
18. Console.WriteLine();
20. // Set the first section to 3: 11
21. bitVector[section1] = 3;
22. Console.WriteLine("New value of section 1: {0}", bitVector[section1]);
24. // Set the second section to 7: 111
25. bitVector[section2] = 7;
26. Console.WriteLine("New value of section 2: {0}", bitVector[section2]);
28. // Set the third section to 11: 1011
29. bitVector[section3] = 11;
30. Console.WriteLine("New value of section 3: {0}", bitVector[section3]);
32. Console.WriteLine();
34. // The value of vector is 383: 00000000 00000000 00000001 01111111
35. Console.WriteLine("The value of vector is {0} represented as \n {1}",
36. bitVector.Data, bitVector.ToString());


Here I create a new vector initialize to zero then build three sections of different sizes. Note that the CreateSection method is creating a new section with the number of bits represented by the integer not the integer itself, so in the above code I’ve created three sections of 2, 3, and 4 bits length respectively all set to zero initially. In the next step I set each section to a specific integer and the binary representation of that integer will be put in the section. The tricky part is here to avoid using an integer that requires more bits that the section defined which can cause unexpected results. In the last step I print out the vector value that consists of the sections defined for it.



BitArray and BitVector32 are two lesser-known collection types in the .NET Framework to deal with small integers and bits where BitVector32 is more efficient as its has a fixed size and BitArray can have an indefinite size.


Published at DZone with permission of Keyvan Nayyeri. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}