C中的位计数类似于bit twiddling hack

问题描述 投票:2回答:3

我需要创建一个例程来计算一个不涉及循环的字中的位(仅位操作),并且不使用大常量。

int x = 0xFFFFFFFF;
x += (~((x >> 1) & 0x55555555)+1);
x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
x = (((x >> 4) + x) & 0x0F0F0F0F);
x += (x >> 8);
x += (x >> 16);
return(x & 0x0000003F);

这是我发现的有点笨拙的黑客,但我可以使用的最大常量是0xFF ...不知道如何做到这一点否则。

谢谢大家。

c bit-manipulation counter bit
3个回答
4
投票

例如,您可以使用常量数组COUNTS[16],它是从0到15的数字的二进制表示中的设置位数。然后:

static inline int byte_count (int x) {
  static const int COUNTS[16] = { 0, 1, 1, 2, 1, /* fill in the rest manually */ };
  return COUNTS[x & 15] + COUNTS[x >> 4];
}

int count(int x) {
  return byte_count(x >> 24) + byte_count((x >> 16) & 255) + byte_count((x >> 8) & 255) + byte_count(x & 255);
}

没有循环,没有大于255的常量。


3
投票

Using your algorithm:

int x = 0xFF;
x |= (x << 8);  // x = 0xFFFF
x |= (x << 16); // x = 0xFFFFFFFF

然后是其余代码 - 只要它有效。

Recursive solution:

int foo ( int x )
{
    if ( x == 0 )
        return 0;
    return (x & 1) + foo ( x/2 );
}

0
投票

你的问题已经回答了here

int NumberOfSetBits(int i)
{
    i = i - ((i >> 1) & 0x55555555);
    i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
    return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
}
© www.soinside.com 2019 - 2024. All rights reserved.