title: interleaving bits with a mask
body: <p>inputs:</p>
  
  <pre><code>arbitrary bitset, e.g. bit positions 012345
  arbitrary bit mask, e.g. (x=1) xx0x0x
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>xx0x1x2345
  </code></pre>
  
  <p>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.</p>
  
  <p>example:</p>
  
  <pre><code>mask = 1001001
  bits = 1101
  result = 1111011
  </code></pre>
  
  <p>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.</p>
  
  <p>edit: I've looked at the algorithms at <a href="http://graphics.stanford.edu/~seander/bithacks.html" rel="nofollow noreferrer">http://graphics.stanford.edu/~seander/bithacks.html</a> and <a href="http://www.hackersdelight.org/HDcode.htm" rel="nofollow noreferrer">http://www.hackersdelight.org/HDcode.htm</a>, but haven't found the exact method yet.</p>