title: Extracting bits with a single multiplication
body: <p>I saw an interesting technique used in an <a href="https://stackoverflow.com/a/14539116/367273">answer</a> to <a href="https://stackoverflow.com/questions/14537831/isolate-specific-row-column-diagonal-from-a-64-bit-number">another question</a>, and would like to understand it a little better.</p>
  
  <p>We're given an unsigned 64-bit integer, and we are interested in the following bits:</p>
  
  <pre><code>1.......2.......3.......4.......5.......6.......7.......8.......
  </code></pre>
  
  <p>Specifically, we'd like to move them to the top eight positions, like so:</p>
  
  <pre><code>12345678........................................................
  </code></pre>
  
  <p>We don't care about the value of the bits indicated by <code>.</code>, and they don't have to be preserved.</p>
  
  <p>The <a href="https://stackoverflow.com/a/14539116/367273">solution</a> was to mask out the unwanted bits, and multiply the result by <code>0x2040810204081</code>. This, as it turns out, does the trick.</p>
  
  <p>How general is this method? Can this technique be used to extract any subset of bits? If not, how does one figure out whether or not the method works for a particular set of bits?</p>
  
  <p>Finally, how would one go about finding the (a?) correct multiplier to extract the given bits?</p>