#include<iostream.h>
int func(int x){
int count=0;
while(x){
count ++;
x=x&(x-1);
}
return count;
}
void main(){
cout <<func(9999);
}
int func(int x){
int count=0;
while(x){
count ++;
x=x&(x-1);
}
return count;
}
void main(){
cout <<func(9999);
}
本文介绍了一个使用C++编写的简单函数,该函数可以计算任意整数中1的个数。通过位操作的方法,此函数提供了一种高效的方式来进行计数。文章包含完整的代码示例。
1971

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



