C++ Bitset 中的 count()函数

首页 / C++入门教程 / C++ Bitset 中的 count()函数

C++ bitset count()函数用于对数字的二进制表示形式中的设置位数进行计数。

count - 语法

int count();

count - 返回值

它以整数值返回设置位数。

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-bitset-count-function.html

来源:LearnFk无涯教程网

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;
}

输出:

1100 has 2 bits

count - 例子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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

邱岳的产品实战 -〔邱岳〕

Go语言核心36讲 -〔郝林〕

接口测试入门课 -〔陈磊〕

SRE实战手册 -〔赵成〕

Go 并发编程实战课 -〔晁岳攀(鸟窝)〕

手机摄影 -〔@随你们去〕

小马哥讲Spring AOP编程思想 -〔小马哥〕

遗留系统现代化实战 -〔姚琪琳〕

零基础GPT应用入门课 -〔林健(键盘)〕

好记忆不如烂笔头。留下您的足迹吧 :)