比特集::计数() 是C++中内置的STL,它返回一个数字二进制表示中的设置位数。
null
语法:
int count()
参数: 该函数不接受任何参数。
返回值: 该函数返回设置位数。如果传递的数字是整数,则返回数字的二进制表示形式中的总位数或设置位数。
下面的程序演示了bitset::count()函数。
项目1:
// CPP program to illustrate the // bitset::count() function #include <bits/stdc++.h> using namespace std; int main() { // Initialisation of a bitset bitset<4> b1(string( "1100" )); bitset<6> b2(string( "001000" )); // Function to count the // number of set bits in b1 int result1 = b1.count(); cout << b1 << " has " << result1 << " set bit" ; // Function to count the // number of set bits in b2 int result2 = b2.count(); cout << b2 << " has " << result2 << " set bit" ; return 0; } |
输出:
1100 has 2 set bit 001000 has 1 set bit
项目2:
// CPP program to illustrate the // bitset::count() function // when the input is an integer #include <bits/stdc++.h> using namespace std; int main() { // Initialisation of a bitset bitset<4> b1(16); bitset<4> b2(18); // Function to count the // number of set bits in b1 int result1 = b1.count(); cout << b1 << " has " << result1 << " set bit" ; // Function to count the // number of set bits in b2 int result2 = b2.count(); cout << b2 << " has " << result2 << " set bit" ; return 0; } |
输出:
0000 has 0 set bit 0010 has 1 set bit
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END