I love a good bit-level puzzle. Today’s is one I learned about from a comment in a Google+ discussion:

Given two words a and b, return the high bits that they have in common, with the highest bit where they differ set, and all remaining bits clear. So 10101 and 10011 yields 10100. Then do the same thing, only flipped, so we keep low-order identical bits and mask out the high ones.

The problem comes from trie implementation. Here’s code that does it the slow way:

uint64_t high_common_bits_64_slow (uint64_t a, uint64_t b) { uint64_t mask = 0x8000000000000000LLU; uint64_t output = 0; int i; for (i=63; i>=0; i--) { if ((a & mask) == (b & mask)) { output |= (a & mask); } else { output |= mask; goto out; } mask >>= 1; } out: return output; } uint64_t low_common_bits_64_slow (uint64_t a, uint64_t b) { uint64_t mask = 1; uint64_t output = 0; int i; for (i=0; i<64; i++) { if ((a & mask) == (b & mask)) { output |= (a & mask); } else { output |= mask; goto out; } mask <<= 1; } out: return output; }

The problem is to do much better than these. To keep things simple, let’s say we’re interested primarily in speed on newish x86-64 implementations. Choose any programming language, or just write assembly. In a few days I’ll post my solutions in addition to any good ones that show up here before then. Beware WordPress’s mangling of source code in the comments — feel free to just mail me.