1. 程式人生 > >函式實現返回引數二進位制中1的個數;無符號的數

函式實現返回引數二進位制中1的個數;無符號的數

int  count_one_bits(unsigned int value)


#include<stdio.h>              //函式返回引數二進位制中1的個數;
#include<stdlib.h>   // 
int count_one_bits(unsigned int value)
{
        int count = 0;
        int ret = 0;
        while (value)
 if (value % 2 == 1)
 {
        count++;
 }
 value = value / 2;
 return count;
}
int main()
{
       unsigned int num = 0;
       int ret = 0;
       ret = count_one_bits(num);
       scanf("%d", &num);
       printf("count=%d", ret);
       system("pause");
       return 0;
}