zoukankan      html  css  js  c++  java
  • C语言 统计整数二进制表示中1的个数

    这是一个很有意思的问题,也是在面试中最容易被问到的问题之一。这个问题有个正式的名字叫Hamming_weight,而且wikipedia上也提供了很好的位运算解决的方法,这个下面也会提到。

    解决这个问题的第一想法是一位一位的观察,判断是否为1,是则计数器加一,否则跳到下一位,于是很容易有这样的程序。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    int test(int n)
    {
        int count=0;
        while(n != 0){
            if(n%2 ==1)
                count++;
            n /= 2;
        }
        return count;
    }

    或者和其等价的位运算版本:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    int test(int n)
    {
        int count=0;
        while(n != 0){
            count += n&1;
            n >>= 1;
        }
        return count;
    }

    这样的方法复杂度为二进制的位数,即,于是可是想一下,有没有只与二进制中1的位数相关的算法呢。

    可以考虑每次找到从最低位开始遇到的第一个1,计数,再把它清零,清零的位运算操作是与一个零,但是在有1的这一位与零的操作要同时不影响未统计过的位数和已经统计过的位数,于是可以有这样一个操作 n&(n-1) ,这个操作对比当前操作位高的位没有影响,对低位则完全清零。拿6(110)来做例子,第一次 110&101=100,这次操作成功的把从低位起第一个1消掉了,同时计数器加1,第二次100&011=000,同理又统计了高位的一个1,此时n已变为0,不需要再继续了,于是110中有2个1。

    代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    int test(int n)
    {
        int count=0;
        while(n != 0){
            n &= n-1;
            count ++;
        }
        return count;
    }

    这几个方法虽然也用到了位运算,但是并没有体现其神奇之处,下面这个版本则彰显位运算的强大能力,若不告诉这个函数的功能,一般一眼看上去是想不到这是做什么的,这也是wikipedia上给出的计算hamming_weight方法。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    int test(int n)
    {
        n = (n&0x55555555) + ((n>>1)&0x55555555);
        n = (n&0x33333333) + ((n>>2)&0x33333333);
        n = (n&0x0f0f0f0f) + ((n>>4)&0x0f0f0f0f);
        n = (n&0x00ff00ff) + ((n>>8)&0x00ff00ff);
        n = (n&0x0000ffff) + ((n>>16)&0x0000ffff);
      
        return n;
    }

    没有循环,5个位运算语句,一次搞定。

    比如这个例子,143的二进制表示是10001111,这里只有8位,高位的0怎么进行与的位运算也是0,所以只考虑低位的运算,按照这个算法走一次

    +---+---+---+---+---+---+---+---+
    | 1 | 0 | 0 | 0 | 1 | 1 | 1 | 1 |   <---143
    +---+---+---+---+---+---+---+---+
    |  0 1  |  0 0  |  1 0  |  1 0  |   <---第一次运算后
    +-------+-------+-------+-------+
    |    0 0 0 1    |    0 1 0 0    |   <---第二次运算后
    +---------------+---------------+
    |        0 0 0 0 0 1 0 1        |   <---第三次运算后,得数为5
    +-------------------------------+

    这里运用了分治的思想,先计算每对相邻的2位中有几个1,再计算每相邻的4位中有几个1,下来8位,16位,32位,因为2^5=32,所以对于32位的机器,5条位运算语句就够了。

    像这里第二行第一个格子中,01就表示前两位有1个1,00表示下来的两位中没有1,其实同理。再下来01+00=0001表示前四位中有1个1,同样的10+10=0100表示低四位中有4个1,最后一步0001+0100=00000101表示整个8位中有5个1。

    计算m~n之间所有数的二进制数位上的1的个数:

     1 #include <stdio.h>
    2 int find(int x)
    3 {
    4 int l=1;
    5 while(l<x)
    6 {
    7 l=l<<1;
    8 }
    9 return l-x<x-l/2?l:l/2;
    10 }
    11 int bf(int x)
    12 {
    13 int s=0;
    14 while(x)
    15 {
    16 if(x&1)
    17 ++s;
    18 x=x>>1;
    19 }
    20 return s;
    21 }
    22 int main()
    23 {
    24 int m,n,i,l,t1,t2;
    25 long long a[29]={1},k;
    26 for(i=1,k=1;i<=28;i++)
    27 {
    28 a[i]=2*a[i-1]+k-1;
    29 k=k<<1;
    30 }
    31 while(scanf("%d%d",&n,&m)!=EOF)
    32 {
    33 k=0;
    34 t1=find(n);
    35 t2=find(m);
    36 if(t1>=n)
    37 for(i=n;i<=t1;i++)
    38 k=k+bf(i);
    39 else
    40 for(i=t1+1;i<n;i++)
    41 k=k-bf(i);
    42 if(t2>=m)
    43 for(i=m+1;i<=t2;i++)
    44 k=k-bf(i);
    45 else
    46 for(i=t2+1;i<=m;i++)
    47 k=k+bf(i);
    48 i=0;
    49 while(t1)
    50 {
    51 t1=t1>>1;
    52 ++i;
    53 }
    54 k=k-a[i-1];
    55 i=0;
    56 while(t2)
    57 {
    58 t2=t2>>1;
    59 ++i;
    60 }
    61 k=k+a[i-1];
    62 printf("%lld\n",k);
    63 }
    64 }

    另一个更犀利的方法:

    #include <stdio.h>
    int main()
    {
    int m,n,i,l,t1,t2,k;
    long long sum1,sum2,s;
    long long a[33]={1};
    for(i=1,k=1;i<33;i++)
    {
    a[i]=2*a[i-1]+k-1;
    k=k<<1;
    }
    while(scanf("%d%d",&n,&m),m+n)
    {
    n--;
    k=1;
    s=sum1=i=0;
    i=0;
    while(n)
    {
    if(n&1)
    {
    s=s+k;
    sum1=sum1+s+a[i];
    }
    i++;
    n=n>>1;
    k=k<<1;
    }
    sum1=sum1-s;
    k=1;
    s=sum2=i=0;
    while(m)
    {
    if(m&1)
    {
    s=s+k;
    sum2=sum2+s+a[i];
    }
    i++;
    m=m>>1;
    k=k<<1;
    }
    sum2=sum2-s;
    printf("%lld\n",sum2-sum1);
    }
    }


    此方法的递归实现:

    #include <cstdio>
    #include <cstring>

    using namespace std;

    int a, b;

    long long go(int n, int m)
    {
    if((n >> m) == 0)
    return 0;
    int d = (n >> m) & 1;
    if(d == 1)
    return (n >> (m + 1)) * (1 << m) + (n & ((1 << m) - 1)) + 1 + go(n, m + 1);
    else
    return (n >> (m + 1)) * (1 << m) + go(n, m + 1);
    }

    int main()
    {
    while(scanf("%d%d", &a, &b) == 2)
    {
    printf("%lld\n", go(b, 0) - go(a - 1, 0));
    }

    return 0;
    }



  • 相关阅读:
    json转换成对象
    查看本机电脑的dns
    springboot整合jsp
    mysql DATE_FORMAT()函数用法
    checkbox全选反选
    mysql 中GROUP_CONCAT使用
    checkeds 选中获取tbale表格中某一列td标签中的值
    工具方法整理
    java开发微信公众号支付(JSAPI)
    Could not create connection to database server. Attempted reconnect 3 times. Giving up.错误
  • 原文地址:https://www.cnblogs.com/AkQuan/p/2279404.html
Copyright © 2011-2022 走看看