Probably the simplest version is<div><br></div><div><div>magic = 0x07EDD5E59A4E28C2</div><div>offset = 58</div></div><div><br></div><div><div>lsbTable :: UArray Word64 Word8</div><div>lsbTable = listArray (0, 63)</div><div>
   [ 63, 0,  58, 1,  59, 47, 53,  2</div><div>   , 60, 39, 48, 27, 54, 33, 42,  3</div><div>   , 61, 51, 37, 40, 49, 18, 28, 20</div><div>   , 55, 30, 34, 11, 43, 14, 22,  4</div><div>   , 62, 57, 46, 52, 38, 26, 32, 41</div>
<div>   , 50, 36, 17, 19, 29, 10, 13, 21</div><div>   , 56, 45, 25, 31, 35, 16,  9, 12</div><div>   , 44, 24, 15,  8, 23,  7,  6,  5</div><div>   ]</div><div><br></div><div>foldrBits :: (Int -&gt; a -&gt; a) -&gt; a -&gt; Word64 -&gt; a</div>
<div>foldrBits f z 0 = z</div><div>foldrBits f z n | t &lt;- n .&amp;. negate n = f (fromIntegral (lsbTable ! shiftR (t * magic) offset)) (foldrBits f z (xor n t))</div><div><br></div><div>swapping to </div><div><br></div>
<div><pre>lsbTable = listArray (0,31) [0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9]</pre><pre>magic = 0x077CB531</pre><pre>offset = 27</pre><pre><font face="arial, helvetica, sans-serif">for the 32 bit case.</font></pre>
<pre><span style="font-family: arial, helvetica, sans-serif; "><br></span></pre><pre><span style="font-family: arial, helvetica, sans-serif; ">-Edward</span></pre></div></div>