BitVector32 structure and BitVector32.Section structure
Part of my 2008 study notes for exam 70-536 (.NET Framework 2.0).
Overview
- Used to manipulate the bits of a 32 integer.
- Used to pack multiple small numbers into one when storage is an issue.
Examples
Bitwise Manipulation
BitVector32 c = new BitVector32(0);
int firstBit = BitVector32.CreateMask();
int secondBit = BitVector32.CreateMask(firstBit);
int thirdBit = BitVector32.CreateMask(secondBit);
c[firstBit] = true;
c[thirdBit] = true;
Console.WriteLine(c.Data); // 5
Console.WriteLine(c); // BitVector32{00000000000000000000000000000101}
Bit Packing
BitVector32.Section a = BitVector32.CreateSection(10); // Max number to store is 10
BitVector32.Section b = BitVector32.CreateSection(80, a); // Max number to store is 80
BitVector32.Section c = BitVector32.CreateSection(5, b); // Max number to store is 5
BitVector32 x = new BitVector32(0);
x[a] = 5;
x[b] = 79;
x[c] = 3;
Console.WriteLine(x[a]); // 5
Console.WriteLine(x[b]); // 79
Console.WriteLine(x[c]); // 3
Console.WriteLine(x.Data); // 7413
Console.WriteLine(x); // BitVector32{00000000000000000001110011110101}
You May Also Like
ArrayList Class
Resizeable, unordered, index-based collection. Suffers from boxing/unboxing when working with value types. Implements IEnumerator, allowing foreach…
BinaryReader Class and BinaryWriter Class
Used to read and write binary data from streams. Call the correct Read methods when using a BinaryReader.
BitArray Class
Contains an array of true/false values. Resizable through the Length property. Supports bitwise operations.



