---
title: "BitVector32 in the real world"
url: "https://osherove.com/blog/2003/7/18/bitvector32-in-the-real-world.md"
canonical: "https://osherove.com/blog/2003/7/18/bitvector32-in-the-real-world.html"
published: "2003-07-18T20:35:00"
updated: "2003-07-18T20:35:00"
tags: ["Cool Articles"]
author: "Roy Osherove"
description: "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…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# BitVector32 in the real world

By Roy Osherove July 18, 2003 In [Cool Articles](https://osherove.com/blog/category/cool-articles)

**Historical article** · Originally published July 18, 2003; technical details may be outdated.

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](http://www.aspzone.com/Entries/cde44ebd07c447a886f2f5b6ddb90d1c.aspx) 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](http://www.msjogren.net/) 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_](http://thetechnologist.is-a-geek.com/blog/) _has created a_ [_replacement for the BitVector32 class_](http://thetechnologist.is-a-geek.com/blog/PermaLink.aspx?guid=fa184c52-b602-4838-95e1-00c7fca09d8d) _in the framework. The original has a nasty bug in it :_ [_the indexer always returns false for bit #31_](http://weblogs.asp.net/rosherove/posts/10270.aspx)_. 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_](http://thetechnologist.is-a-geek.com/blog/PermaLink.aspx?guid=fa184c52-b602-4838-95e1-00c7fca09d8d)_._

\[via a [future post of mine](http://weblogs.asp.net/rosherove/posts/37166.aspx)\]

## More in Cool Articles

[All posts in Cool Articles](https://osherove.com/blog/category/cool-articles)

- Jul 22, 2003

    ### [My article on MSDNAA](https://osherove.com/blog/2003/7/22/my-article-on-msdnaa.html)

- Jul 19, 2003

    ### [Log4net How-To](https://osherove.com/blog/2003/7/19/log4net-how-to.html)

- Jul 18, 2003

    ### [Nice performance counters article](https://osherove.com/blog/2003/7/18/nice-performance-counters-article.html)

- Jul 1, 2003

    ### [User Interface Design For Programmers](https://osherove.com/blog/2003/7/1/user-interface-design-for-programmers.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
