Bit interleaving in objective-c
I would like to learn what is bit interleaving. I have an example:
a: 011
b: 101
---
c: 100111
As far as I see the bits of the number "a" are on pair positi开发者_运维技巧ons in the result and the bits of the number "b" are on impair positions. But why the interleaving starts with number "b"? Do anybody know the rule for this?
I will code the rule in objective-c, so any optimizations connected to this language is very welcome.
Thank you!
I found it!
long x = 3; // Interleave bits of x and y, so that all of the
long y = 5; // bits of x are in the even positions and y in the odd;
long long z = 0; // z gets the resulting Morton Number.
for (int i = 0; i < sizeof(x) * CHAR_BIT; i++) {
z |= (x & 1U << i) << i | (y & 1U << i) << (i + 1);
}
This code does the bit interleaving. As you see the result is a morton number. I hope this will help somebody! :)
精彩评论