I’m looking for an operator based way of working with bit masks and bitwise boolean operations (XOR / NOR / OR / AND / NOT / EQV / etc). Generally I really like an extension method style approach, but in this case, I find it a little messy.
Is there a terser way of working with bits in C#?
BitArray a = new BitArray(0x001);
BitArray b = new BitArray(0x100);
BitArray c = new BitArray(0x010);
BitArray test = a | b; // won't compile
BitArray test2 = a ^ c; // won't compile
BitArray test3 = a.Or(b); // compiles
BitArray test4 = a.Xor(c); // compiles
There’s no way of doing it directly with
BitArray– but you could always create a wrapper class which contained aBitArray, and define your own operators there.Of course, if you’re dealing with 64 bits or fewer, you could just use a
longorulongand be done with it…