#include "stdio.h"
int func(int x){
int count =0;
while(x){
count++;
x=x&(x-1);
}
return count;
}
本文介绍了一个计算整数二进制表示中1的位数的C语言函数。该函数采用位操作技巧,通过不断将数字与其减1后的结果进行按位与运算,直至结果为0,期间每完成一次运算就使计数加1,以此来统计1的个数。
#include "stdio.h"
int func(int x){
int count =0;
while(x){
count++;
x=x&(x-1);
}
return count;
}

被折叠的 条评论
为什么被折叠?