efficiently find the first element matching a bit mask
I have a list of N 64-bit integers whose bits represent small sets. Each integer has at most k bits set to 1. Given a bit mask, I would like to find the first element in the list that matches the mask, i.e. element & mask == element . Example: If my list is: index abcdef 0 001100 1 001010 2 001000 3 000100 4 000010 5 000001 6 010000 7 100000 8 000000 and my mask is 111000 , the first element matching the mask is at index 2. Method 1: Linear search through the entire list. This takes O( N ) time and O(1) space. Method 2: Precompute a tree of all possible masks, and at each node keep the answer