简体   繁体   English

计算无符号整数中的位数

[英]Count number of bits in an unsigned integer

I want to write a function named bitCount() in the file: bitcount.c that returns the number of bits in the binary representation of its unsigned integer argument.我想在文件中编写一个名为bitCount()的函数: bitcount.c ,它返回其无符号整数参数的二进制表示中的位数。

Here is what I have so far:这是我到目前为止所拥有的:

#include <stdio.h>

int bitCount (unsigned int n);

int main () {
    printf ("# 1-bits in base 2 representation of %u = %d, should be 0\n",
        0, bitCount (0));
    printf ("# 1-bits in base 2 representation of %u = %d, should be 1\n",
        1, bitCount (1));
    printf ("# 1-bits in base 2 representation of %u = %d, should be 16\n",
        2863311530u, bitCount (2863311530u));
    printf ("# 1-bits in base 2 representation of %u = %d, should be 1\n",
        536870912, bitCount (536870912));
    printf ("# 1-bits in base 2 representation of %u = %d, should be 32\n",
        4294967295u, bitCount (4294967295u));
    return 0;
}

int bitCount (unsigned int n) {
    /* your code here */
}

Okay, when I just run this I get:好的,当我运行这个时,我得到:

# 1-bits in base 2 representation of 0 = 1, should be 0
# 1-bits in base 2 representation of 1 = 56, should be 1
# 1-bits in base 2 representation of 2863311530 = 57, should be 16
# 1-bits in base 2 representation of 536870912 = 67, should be 1
# 1-bits in base 2 representation of 4294967295 = 65, should be 32

RUN SUCCESSFUL (total time: 14ms)

It doesn't return the correct numbers of bits.它不会返回正确的位数。

What's the best way to return the number of bits in the binary representation of its unsigned integer argument in C?在 C 中返回其无符号整数参数的二进制表示中的位数的最佳方法是什么?

Here's a solution that doesn't need to iterate.这是一个不需要迭代的解决方案。 It takes advantage of the fact that adding bits in binary is completely independent of the position of the bit and the sum is never more than 2 bits.它利用了这样一个事实,即以二进制形式添加位完全独立于位的位置,并且总和永远不会超过 2 位。 00+00=00 , 00+01=01 , 01+00=01 , 01+01=10 . 00+00=00 , 00+01=01 , 01+00=01 , 01+01=10 The first addition adds 16 different 1-bit values simultaneously, the second adds 8 2-bit values, and each one after does half as many until there's only one value left.第一个加法同时添加 16 个不同的 1 位值,第二个添加 8 个 2 位值,之后的每个加法都减半,直到只剩下一个值。

int bitCount(unsigned int n)
{
    n = ((0xaaaaaaaa & n) >> 1) + (0x55555555 & n);
    n = ((0xcccccccc & n) >> 2) + (0x33333333 & n);
    n = ((0xf0f0f0f0 & n) >> 4) + (0x0f0f0f0f & n);
    n = ((0xff00ff00 & n) >> 8) + (0x00ff00ff & n);
    n = ((0xffff0000 & n) >> 16) + (0x0000ffff & n);
    return n;
}

This is hard-coded to 32 bit integers, if yours are a different size it will need adjusting.这是硬编码为 32 位整数,如果您的大小不同,则需要调整。

 int bitCount(unsigned int n) {

    int counter = 0;
    while(n) {
        counter += n % 2;
        n >>= 1;
    }
    return counter;
 }

Turns out there are some pretty sophisticated ways to compute this as answered here .原来有一些相当复杂的方法来计算这是回答在这里

The following impl (I learned way back) simply loops knocking off the least significant bit on each iteration.下面的实现(我回过头来学习)只是在每次迭代中循环删除最低有效位。

int bitCount(unsigned int n) {

  int counter = 0;
  while(n) {
    counter ++;
    n &= (n - 1);
  }
  return counter;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM