title: Use bit manipulation to convert a bit from each byte in an 8-byte number to a single byte
body: <p>I have a 64-bit unsigned integer. I want to check the 6th bit of each byte and return a single byte representing those 6th bits.</p>
  
  <p>The obvious, "brute force" solution is:</p>
  
  <pre><code>inline const unsigned char Get6thBits(unsigned long long num) {
      unsigned char byte(0);
      for (int i = 7; i &gt;= 0; --i) {
          byte &lt;&lt;= 1;
          byte |= bool((0x20 &lt;&lt; 8 * i) &amp; num);
      }
  
      return byte;
  }
  </code></pre>
  
  <p>I could unroll the loop into a bunch of concatenated <code>|</code> statements to avoid the <code>int</code> allocation, but that's still pretty ugly.</p>
  
  <p>Is there a faster, more clever way to do it?  Maybe use a bitmask to get the 6th bits, <code>0x2020202020202020</code> and then somehow convert that to a byte?</p>