BitVector32 in the real world
A little known(and little documented) class in the framework is the System.Collections.Specialized.BitVector32 . It allows working easily with 32 bit integers, which are used extensively in optimized environments as a way to quickly pass data around.
I had in application in VB6 once that had to talk to a COM server written in C++. The server would send and recieve statuses and messages using only 32 bit constructs. Luckily, I had a cool class that helped me work with it easily(I never was much of a bits and bytes guy), but this was in VB6. It's good to know that BitVector32 exists, and now, John R. Lewis has written a short and to the point article which explains how he used it to parse data in a real application. Great contribution! I just *know* that that article will save me a few headaches in the future.
Update:
Mattias Sjögren comments a very important remark:
FYI, the BitVector32 has a bug in that the indexer always returns false for bit #31, even if it's set. The following code should reproduce it.
class BitVectorBugDemo
{
static void Main()
{
BitVector32 bv = new BitVector32( unchecked((int)0xFFFFFFFF) );
int mask = BitVector32.CreateMask();
for ( int bit = 0; bit < 32; bit++ ) {
Console.WriteLine( "Bit: {0}, Mask: {1:X}, value: {2}", bit, mask, bv[mask] );
if ( bit < 31 ) mask = BitVector32.CreateMask( mask );
}
}
}
Thanks for the tip! I wonder how the hell do I work around that...
Update:
Sebastian Lambla has created a replacement for the BitVector32 class in the framework. The original has a nasty bug in it : the indexer always returns false for bit #31. Sebastian decided he needed more flexibility from it anyway so he recreated it from scratch.
Very cool. Great work.
You can download the class in a zip file from his post.
[via a future post of mine]