C++,学习使用STL

求int型正整数在内存中存储时1的个数

http://www.nowcoder.com/practice/440f16e490a0404786865e99c6ad91c9

看到二进制就要想到bitset

#include<bitset>
#include<iostream>
using namespace std;
int main(){
    int n;
    cin>>n;
    bitset<32> b(n);
    cout<<b.count();
}

全部评论
count() 返回 std::bitset 中值为 1 的位的数量
2 回复 分享
发布于 2023-07-11 20:29 陕西
为什么count函数里面什么参数都不放,都可以计算二进制1的个数呀
1 回复 分享
发布于 2023-02-20 21:35 广东
都用轮子了还不如用__builtin_popcount
点赞 回复 分享
发布于 03-23 14:18 安徽

相关推荐

09-01 21:40
已编辑
同济大学 Java
点赞 评论 收藏
分享
评论
109
16
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务