What is the most efficient way of executing a boolean expression on a bitmap in C or C++? For example, let's say that I have a 4-bit bitmap (a, b, c, d)
. Now, let's say that I have a simple boolean expression like (a AND b) OR (c AND d)
. How should I represent the boolean expression so that I could efficiently apply it to my bitmap? I am looking for a generic solution that can be applied to any boolean expression, not just the one given as example. In other words, I am looking for some way to "compile" the boolean expression into another data structure that could be used to efficiently reduce my bitmap into a boolean.
The bitmap structure is the result of filtering operations on the records of a database. Every record has its own bitmap, and every bit in a bitmap is the result of an individual filtering rule. The boolean expression is used to combine these filtering rules to decide whether the record should be included in the results of a database query. There can be up to 64 individual filtering rules that can be combined by the boolean operation, hence the bitmap can be represented as an unsigned long long int
if necessary.
The solution should be efficient in terms of speed and should not alter the bitmap structure. The conversion of the boolean expression into another structure does not have to be memory-efficient nor fast, because it can be cached (at least in my current use case). The reducing of the bitmap with the transformed boolean expression should be both fast and memory efficient.
Notes:
- The boolean expression is only using nested AND and OR operations (no IF statements).
- The solution should assume the availability of a 64bit CPU.
- The solution should not be CPU-dependent (beside 64bit addressing).
- The solution should not assume the availability of any other particular hardware (eg GPU).
- All the bitmaps are in memory.
- There can be a very large number of bitmaps (billions).
- Bitmaps are updated one at a time.