[math-fun] reversing the order of bits in a word
wds>Knuth gave the following absolutely incredible algorithm, here as C which I tested: //Don Knuth incredible algorithm for reversing a 64-bit word x: uint64 y; x = ((x>>1) & U64(0x5555555555555555)) | ((x & U64(0x5555555555555555)) << 1); y = (x ^ (x>>4)) & U64(0x0300C0303030C303); x ^= y ^ (y<<4); y = (x ^ (x>>8)) & U64(0x00C0300C03F0003F); x ^= y ^ (y<<8); y = (x ^ (x>>20)) & U64(0x00000FFC00003FFF); x ^= y ^ (y<<20); x = (x>>34) | (x<<30); return(x); Good luck figuring it out. And what are the 8, 16, and 32-bit versions of it (if any)? <WDS The Symbolics Lisp Machine (and many of its contemporaries) had a BITBLT (Bit block transfer) instruction capable of Boolean functions on rectangular bit arrays. Our usual test subject was a 1 bit/pixel frame of a Zippy the Pinhead strip. I wrote a little demo called Thrash-Zippy which iteratively subjected this to various BITBLTcherous flips and rotations, the most interesting of which was a bitmatrix transpose (swap-xy), which took k1 lg n for n×n, and used lg n n×n masks, each of which I'm pretty sure took k2 lg n to compute. The kth mask designated all those bits which needed to switch with their counterparts 2^k to their northwest. I recall it startling Leo Guibas, who was writing a computational geometry book at the time, so maybe he wrote it up. Funster Stephen Jones has copies of my old files. I think the functions were in a file named bltfun. --rwg
participants (1)
-
Bill Gosper