c - 将 N 位与接下来的 N 位混合(例如每 4 位)00001111 -> 01010101

标签 c bit

正如这个问题的标题所说,我想知道在整数(尤其是 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 hacksInterleave bits 部分找到一些解决方案。

关于c - 将 N 位与接下来的 N 位混合(例如每 4 位)00001111 -> 01010101,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3585117/

相关文章:

C - for 循环初始值设定项中的 while 运算符

c - C中时间函数的奇怪问题

c - 打印off_t

c - 如何处理SIGHLD

c++ - << C++ 中的运算符?

c - 按位与运算不清楚

c - 使用 3.X linux 内核的 C 中的 Netlink 套接字

Python 数量限制

c - Eratosthenes 位数组筛法

c++ - 我有一个由 0's and 1' 组成的字符串。如何将二进制格式的 0's and 1' 存储在文件中?