#include <stdio.h>
#include <stdlib.h>
int main(){
int num = 10;
int count = 0;
while (num != 0){
if (num % 2 == 1){ //判断num在二进制下的每一位是不是1
++count;
}
num = num >> 1;
}
printf("%d\n", count);
system("pause");
return 0;
}
注意这种方法只能用于正数!