且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C中的位翻转-计数位

更新时间:2023-11-08 16:25:58

我想知道每次检查的***位数是多少

I'm wondering what is the optimal number of bits to examine each time

找出答案的唯一方法是测试.请参阅此问题,以获取有关一次计数32位最快方法的讨论.

The only way to find out is to test. See this question for a discussion of the fastest way to count 32 bits at a time.

此外,如果该数字不是某些预设类型的大小,我该怎么办 走下我的位向量并将指针设置为任何任意数字 超过位数组起始位置的位.

Also, if that number is not the size of some preset type, how can I walk down my bit-vector and set a pointer to be ANY arbitrary number of bits past the starting location of the bit array.

您不能将指针设置为任意位.大多数机器都有字节寻址,有些只能寻址字.

You can't set a pointer to an arbitrary bit. Most machines have byte-addressing, some can only address words.

可以构造一个以任意位开头的单词,如下所示:

You can construct a word starting with an arbitrary bit like so:

long wordAtBit(int32_t* array, size_t bit)
{
    size_t idx = bit>>5;
    long word = array[idx] >> (bit&31);
    return word | (array[idx+1] << (32 - (bit&31));
}