bitcount函数 计算出正整型x中位为1的个数。

int bitcount(unsigned int x){
    int res;
    for (res=0;x!=0;x>>=1)
    {
        if(x&1) res++;
    }
    return res;
}

//利用 表达式x&=(x-1)可以去掉x最右边值为1的二进制位。
int bitcount_version2(unsigned int x){
    int res;
    for (res=0;x!=0;x&=(x-1))
    {
        res++;
    }
    return res;
}

 

相关文章:

  • 2021-08-05
  • 2021-12-19
  • 2022-12-23
  • 2022-12-23
  • 2021-09-10
  • 2021-05-31
  • 2021-08-28
猜你喜欢
  • 2021-08-01
  • 2021-12-23
  • 2021-06-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案