Has anyone released a more robust BitArray for .NET? [closed]
Asked Answered
P

1

12

After struggling to make the .NET BitArray class work for my needs, I decided to look for a more robust open-source or commerical one on the web. To my surprise, I can't find a single one. I see various suggestions for extension methods or ways to work around limits to BitArray functionality, but nothing resembling a BitArray replacement.

Are we all reinventing the wheel by extending or replacing BitArray?

Ideally, a replacement would have some/all of these features:

  1. Implements IList<bool> rather than just ICollection.

  2. Can cast to various other types such as int (for up to 32-bit BitArrays), bool (for one-bit BitArrays), double, etc.

  3. Implements a ToArray type of method that yields a byte array. It may be parameterized for endianness. Since BitArray can be constructed from a byte array parameter in the constructor, it seems like good closure to be able to return it to a byte array.

  4. Ability to extract sub-BitArrays from it. For example, from an array like 111101, you could extract bits 1 to 4, yielding a new BitArray of 1110.

  5. Bit shifting operators.

  6. Handles indefinite lengths of bits (like BitArray does), but can still cast to limited types like int, just like you can cast a long to an int if you know it will fit.

  7. ?__ I bet there are plenty of other items on people's wish lists.

Do you know of any open-source or commercial implementations out there? If it's open source, it would be nice to have a nonreciprocal license such as Apache, MIT, or Ms-Pl.

Postimpressionism answered 27/4, 2011 at 20:16 Comment(1)
Your question is exactly what I am thinking. Any updates to this topic in the mean time?Fade
E
5

Perhaps you are looking for BigInteger in the System.Numerics namespace? It certainly looks like it can do whatever you are asking for.

Ethic answered 27/4, 2011 at 20:32 Comment(6)
I think he's looking for something specifically to do with BitArraysPulsometer
@IDWMaster: The BigInteger class gets you about 70% there, including the bit shifting operations. The other 30% can be fulfilled with some carefully-crafted extension methods.Forgiveness
@Tejs, I was not aware of BigInteger. At first, I was wondering the relevance of that response, but then I saw that it really does meet most (but definitely not all) of my criteria. I need to look into issues of endianness since it is not exactly analogous to a BitArray. Thanks for the response.Postimpressionism
@Dale: Endianness is the ordering of bytes that make up a whole integer. How does an array of bits have endianness?Toadstool
@Tergiver, one of my criteria was to be able to convert the bit array to a byte array, thus the endianess issue.Postimpressionism
To follow up, I tried to use BigInteger, but gave up because there is no BigUnsigned version of it, and BigInteger pads positive numbers with a zero in order to prevent it from looking like a twos-compliment number. This complicated bit manipulation to the point that I gave up on it and went back to manipulating byte arrays.Postimpressionism

© 2022 - 2024 — McMap. All rights reserved.