Interleave bits efficiently
<p>I need to make <code>uint64_t</code> out of 2 <code>uint32_t</code> interleaving the bits: if <code>A=a0a1a2...a31</code> and <code>B=b0b1...b31</code>, I need C=<code>a0b0a1b1...a31b31</code>. Is there a way to do this efficiently? So far I've got only the naive approach with a <code>for</code> loop of 32 iterations, where each iteration does <code>C|=((A&amp;(1&lt;&lt;i))&lt;&lt;i)|((B&amp;(1&lt;&lt;i))&lt;&lt;(i+1))</code>.</p>

<p>I guess there should be some mathematical trick like multiplying A and B by some special number which results in interleaving their bits with zeros in the resulting 64-bit number, so that what only remains is to <code>or</code> these products. But I can't find such multiplier.</p>

<p>Another potential way to go is a compiler intrinsic or assembly instruction, but I don't know of such.</p>