C++ count()
C++ count()函数
C++ count()函数用于对数字的二进制表示形式中的设置位数进行计数。
语法
参数
不带任何参数。
返回值
它返回将位设置为整数值。
示例1
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
bitset<4> b1(string("1100"));
int result=b1.count();
cout<<b1 << " has" <<' ' << result <<" bits";
return 0;
}
输出:
示例2
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
bitset<4> b1(16);
bitset<4> b2(18);
int result=b1.count();
int result1=b2.count();
cout<<b1 << " has" <<' ' << result <<" set bits" << '\n';
cout<<b2 << " has" <<' ' << result1 <<" set bits";
return 0;
}
输出:
0000 has 0 set bits
0010 has 1 set bits