barpas
barpas

Reputation: 75

c# fastest way to do bitwise operations on large block of bits

I have a set of arrays of hundreds of thousands flags 0 or 1.

I'm using the BitArray class for doing something like that:

result = (BitArray)ab.Clone();
result.And(bc);

many many times...

Of course I have to set the flags in Bitarray first.

for (int i = 1; i < maxLen; i++) ab[i] = a[i] < b[i];

But when I set the flags once then I'm doing thousands of operations and, or, xor, not on them (so the speed of the bitwise operations is much more important)

And again from the beginning.

I'm asking you if is in C# faster method for doing this?

Upvotes: 1

Views: 1331

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1064214

Assuming GPU isn't an option, your best bet will be Vector<byte> on a CPU that supports SIMD operations. There are methods in System.Runtime.CompilerServices.Unsafe that allow you to load a Vector<T> from an unsafe pointer (specifically: Unsafe.Read<Vector<byte>>), allowing it to be used very efficiently against arrays pinned via fixed. Or: wait until Span<T> is mainstream, and use NonPortableCast<byte, Vector<byte>>

Upvotes: 4

Related Questions