BitVector32 in .NET
Join the DZone community and get the full member experience.
Join For Freebit 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()
02.{
03. // initialize the vector to 2: 00000000 00000000 00000000 00000010
04. bitvector32 bitvector = new bitvector32(2);
05.
06. console.writeline("initially, the value of vector is {0} represented as \n {1}",
07. bitvector.data, bitvector.tostring());
08.
09. int bit1 = bitvector32.createmask();
10. int bit2 = bitvector32.createmask(bit1);
11. int bit3 = bitvector32.createmask(bit2);
12.
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());
17.
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());
22.
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());
27.}
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.
sections
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()
02.{
03. // initialize the vector to 0: 00000000 00000000 00000000 00000000
04. bitvector32 bitvector = new bitvector32(0);
05.
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]);
09.
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]);
13.
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]);
17.
18. console.writeline();
19.
20. // set the first section to 3: 11
21. bitvector[section1] = 3;
22. console.writeline("new value of section 1: {0}", bitvector[section1]);
23.
24. // set the second section to 7: 111
25. bitvector[section2] = 7;
26. console.writeline("new value of section 2: {0}", bitvector[section2]);
27.
28. // set the third section to 11: 1011
29. bitvector[section3] = 11;
30. console.writeline("new value of section 3: {0}", bitvector[section3]);
31.
32. console.writeline();
33.
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());
37.}
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.
conclusion
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.
Comments