BitVector32 structure and BitVector32.Section structure

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}