0

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?

2
  • 2
    BigInteger is 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. Commented Feb 27, 2014 at 10:03
  • 1
    There is the BitArray class, designed for this. Using List<byte> is pretty easy as well. If you're willing to sacrifice memory, List<bool> is another alternative. Commented Feb 27, 2014 at 10:04

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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>.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.