Search: in  

Rob Kennedy's Blog

Rob Kennedy is an avid and active .NET software developer who tends to have an opinion about everything. In the windows developer community for over 10 years, he has grown his career and opinions through experience.

My BitVector kicks your BitArray's Ass

So I was stumbling through the MSDN docs today (because I'm a goob) and came across a gem I had not seen before. BitVector32. Now for those of you who have no need to do any sort of bit manipulation, evaluation, etc. please move on, but for those of us who do/have done serial communications, or binary data manipulation, this is a real nice interface to have.

The old and busted way (ok, I still use this) was to use operator masks to determine if a bit was flipped or not. For example if I was trying to see if the 3rd bit (going from right to left) was set on I could say isOn = (myBits And 4) and it would return a non-zero value if the bit was on.

With the BitArray, we could break out our data variable into an array of bits and grab the individual boolean value based on the array index. This would also simplify very large bit masks, but the overhead of a class and dynamic array creation could lead to a performance hit on performance-hungry applications.

This leads to the BitVector32 structure which is a lightweight toolset to easily manipulate bits programmatically within the confines of a double word (32 bits) which is usually more than enough.

Check it out if you don't believe me.

http://msdn2.microsoft.com/en-us/library/system.collections.specialized.bitvector32.createmask(VS.71).aspx

 

Update: I also wanted to add that after writing this blog, I decided to waste some time and write a 64-bit version of this structure since I could not find an existing library, but did find requests for one on the web.

Check it out here: http://robkennedy.com/files/folders/web_libraries/entry102.aspx

Share this post: E-mail My+BitVector+kicks+your+BitArray%27s+Ass | Submit My+BitVector+kicks+your+BitArray%27s+Ass to Technorati | Submit My+BitVector+kicks+your+BitArray%27s+Ass to del.icio.us | Submit My+BitVector+kicks+your+BitArray%27s+Ass to digg.com | Submit My+BitVector+kicks+your+BitArray%27s+Ass to reddit.com | Submit My+BitVector+kicks+your+BitArray%27s+Ass to DotNetKicks | Add My+BitVector+kicks+your+BitArray%27s+Ass to Live Bookmarks

Comments

No Comments

About RobK410

I'm generally into the three c's: Cars, Chicks, and Computers.
Copyright © 2007, RobKennedy.com.
I reserve the right to beat yo arse if you steal sh!t.
Shat out at you via the CommunityServer.org Engine