TL;DR:
How can I quickly find the smallest integer that satisfies int > floor && ((int ^ bits) & mask) == 0, where floor, bits and mask are known? (Ideally a non-looping, non-branching solution, probably with some serious bit-twiddling involved.)
Background
Suppose I have a list of key-values sorted by key, where each key is an integer. I want to quickly gather each value whose key has certain flags set or clear, i.e. matches the filter (key & mask) == (bits & mask) or equivalently ((key ^ bits) & mask) == 0.
In a naïve implementation, I could iterate over every key-value and gather those matching the filter. However, if my matches are sparse, I'd like to take advantage of my list's sortedness to quickly find new matches.
So my current approach is an algorithm that switches back and forth between binary-searching and scanning the list, maintaining the smallest possible future key based on the last key read in. If a search for the key succeeds, it scans until the the read-in key no longer matches the filter. Either way, the search begins anew with a newly computed "smallest possible future key" based on the last key we read in (aka floor, which doesn't match the filter.)
My testing has shown the algorithm to be correct (thankfully), but I'm using a placeholder iterative method to compute the "next" key instead of a bit-twiddling method like I was hoping to have come up with... which brings us back to the TL;DR!