I know a similar question has already been asked. However, I have some trouble manipulating bits and can't adapt/understand the solution.
First of all I used the Interleave bits the obvious way to get myself a Morton number. I adapted it to 3D like this:
unsigned long xyz2w(unsigned int x, unsigned int y, unsigned int z)
{
unsigned long w=0;
for (int i = 0; i < sizeof(x) * CHAR_BIT; i++)
{
w |= (x & 1U << i) << i | (y & 1U << i) << (i + 1) | (z & 1U << i) << (i + 2);
}
return w;
}
(It seems to work fine but if you ever find an error, please let me know)
Now I would like to do the opposite operation. That is: extracting the 3 coordinates based on the Morton number.
Obviously, I tried to use the adapted-for-3D part of the previous SO topic, but I didn't understand how it works. That's why I am creating a new topic.
I am not looking for a high-performance code but for a clear and understandable code that works similar to the xyz2w function (ie "The obvious way").