正如这个问题的标题所说,我想知道在整数(尤其是 64 位无符号)中混合位 block 的最佳方法
for example I have 8bit integer, where it's bits are 0000 1111 mix 4bits by 4 bits = 0101 0101
example 2: 0010 0110
0 1 1 0 right 0.0.1.0 left = 00011100 mix 4bits by 4 bits = 0001 1100 Simple is, . places filled with bits of right block
我现在在做什么:
uint64_t mix32(uint64_t v) {
uint64_t ret=0;
int x=0;
for(int i=0; i<32; i++) {
setbit(ret, x, getbit(v, i));
x++;
setbit(ret, x, getbit(v, i+32));
x++;
}
return ret;
}
其中 setbit 是设置或清除特定位置上的位的宏。 我真正需要的是 将每个 32 位与接下来的 32 位混合 将每个 16 位与接下来的 16 位混合 将每个 16 位与下一个 16 位混合 将每个 8 位与接下来的 8 位混合 ETC... 如果有这样的位操作示例,我希望我可以休息。我在谷歌上看了很多,但最终得到的教程没有展示这种情况。
保持健康。
最佳答案
参见 Bit twiddling hacks在 Interleave bits 部分找到一些解决方案。
关于c - 将 N 位与接下来的 N 位混合(例如每 4 位)00001111 -> 01010101,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3585117/