Bitwise operation in large list as fast as possible in c#

c#, performance

Solution

If I understand your question correctly, you want to check `a` against each `b` whether some predicate is true. So a naive solution to your problem would be as follows:

var result = aList.Sum(a => bList.Count(b => (a & b) == a));

I'm not sure this can really be sped up for an arbitrary predicate, because you can't get around checking each `a` against each `b`. What you could try is run the query in parallel:

var result = aList.AsParallel().Sum(a => bList.Count(b => (a & b) == a));

Example:

`aList`: 10,000 random `long` values; `bList`: 100,000 random `long` values.

without `AsParallel`: 00:00:13.3945187

with `AsParallel`: 00:00:03.8190386

Problem

I have a list from 10,000 long value and I want to compare that data withe 100,000 other long value compare is a bitwise operation --> ``` if (a&b==a) count++; ``` which algoritm I can use for getting best performance?

Original source