I am looking for a small manipulation function that takes two bit strings and filters and compacts the first string based on the second, so only the values ββare saved where the second string is 1. For example:
01101010 and 11110000 gives 00000110 01101010 and 00001111 gives 00001010 01101010 and 10101000 gives 00000011
Using a loop, conditional expressions, and working with each bit independently, this is easy to implement, but I'm looking for a faster method using techniques for manipulating bits if it exists, rather than using conditional expressions and loops . It should not work for input over 32 bits. Therefore, the solution should have such a signature as: uint32_t filter (uint32_t in, uint32_t mask)
In C, it will look something like this: arrays and a loop:
void filter(bool in[], bool mask[], bool out[], int size) { int output_index = 0; for (int input_index = 0; input_index < size; ++input_index) { if (mask[input_index]) { out[output_index++] = in[input_index]; } } }
Here are some examples of the types of solutions I'm looking for: Twiddling Hacks Bit
source share