c语言中统计整数类数据类型二进制表示时一共包含1的个数

 

001、

#include <stdio.h>

int main(void)
{
    unsigned int x;
    printf("x = "); scanf("%u", &x);                   // 输出整数类数据
    int count = 0;
    
    while(x)
    {
        if(x & 1U)                                     // 判断x二进制表示时末尾是否为1;
        {
            count++;
        }
        x >>= 1;                                       // while循环条件更新,循环一次,右移一位
    }
    
    printf("count = %d\n", count);
    
    return 0;
}

 

 

posted @ 2022-08-18 01:05  小鲨鱼2018  阅读(72)  评论(0编辑  收藏  举报