If I have two 32-bit values, X and Y, how can I efficiently interleave their bits into one 64-bit value Z, in the order xyxyxyxy... (Z is a position on a Z-order curve.)
I could iterate over each of the bits in X and Y, setting bits in Z as I go. This seems inefficient.
Is there a shortcut to interleaving the bits from two values into one large value, which takes less than a hundred CPU instructions?
This C++-answer also applies to C: https://stackoverflow.com/a/39490836/11993121
The answer outlines the principle, but does not write out the complete solution. A working implementation is as follows:
Example of test: