I would like to use System.Numeric.BigInteger as bit field.
I have flags in range 1 - 2^255.
Is there any performance reason (memory or speed) to write custom container or I can just use BigInteger for this?
1 Answer
You can use BitArray, which is tailored for that kind of storage, and after if you need to store, you can store it like a sequence of chars, or use some other serialization option.
2 Comments
CodesInChaos
That class is a bit weird. For some reason it doesn't implement
IList, and it wasn't modified to add generic interfaces either. I'd expect it to implement IList<bool>.Tigran
@CodesInChaos: probably the idea of it is to process stream of bit values. in CLR when ms wants to manifest such usecase of a type, usually stops on implementing it from IEnumerable, or at maximum, from ICollection.
BigIntegeris immutable, so any change needs to allocate a new one. But unless you're using this in a tight loop or with large values (much larger than 256 bits), it shouldn't matter.BitArrayclass, designed for this. UsingList<byte>is pretty easy as well. If you're willing to sacrifice memory,List<bool>is another alternative.