inputs:
arbitrary bitset, e.g. bit positions 012345
arbitrary bit mask, e.g. (x=1) xx0x0x
output:
xx0x1x2345
That is, I want the first bit of the bitset to be placed in the first 0 of the mask. Likewise, the second bit is placed in the second 0 of the mask.
example:
mask = 1001001
bits = 1101
result = 1111011
I know that this can be done with a loop, but I'd like do it using primarily bit operations. I know you can perform arbitrary bit permutations using only masking and bit operators. I'm willing to spend a good amount of time setting up the permutation masks since the input mask will be used many times.
edit: I've looked at the algorithms at http://graphics.stanford.edu/~seander/bithacks.html and http://www.hackersdelight.org/HDcode.htm, but haven't found the exact method yet.