求32位二进制数中有多少个1或者多少个0
#include <stdio.h>
int func(int x);
int main()
{
printf("%d\n", func(254));
return 0;
}
int func(int x)
{
int countx = 0;
while(x)
{
countx++;
/* 求x的二进制表达式有多少个1 */
// x = x & (x - 1);
/* 求x的二进制表达式有多少个0 */
// x = x | (x - 1);
x = x & (x - 1);
}
return countx;
}