Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Bitwise operations on large integers

I have many flags that I want to store them in an integer, so that later I can perform bitwise AND operation on them. The number of these flags may be up to 1000. However, there are no types in C# that can store 1000 bits.

For Example, I want to perform (&) operation onü

100000010000000011100000000000000000000000000000...........00001 --> (1000 flags)
000000000000000001111110000000000000000000010101.......... 11111 --> (1000 flags)

So, how can I perform this (&) operation on a two bit collections?

like image 303
Ahmet Altun Avatar asked Oct 25 '25 23:10

Ahmet Altun


2 Answers

I have many flags that I want to store them in an integer, so that later I can perform bitwise AND operation on them.

That's a non-sequitur. You can use BitArray with its And method to satisfy that requirement without artificially going via a numeric type.

If you haven't logically got a number (and "many flags" doesn't sound like a number to me) you shouldn't go out of your way to use a numeric representation.

like image 87
Jon Skeet Avatar answered Oct 28 '25 15:10

Jon Skeet


I guess BigInteger is what you are looking for. It has BitwiseAnd operator

like image 39
ie. Avatar answered Oct 28 '25 14:10

ie.