题解 | 二进制位中1的数量

二进制位中1的数量

https://www.nowcoder.com/practice/3e392d0ed7e543f3a4ae883d1470ca9d

#include <iostream>
using namespace std;
#define int long long

signed main() {
    int n;cin>>n;
    int cnt=0,i=0;
    while(i<64){//long long时最大数对应的二进制位
        if((n>>i)&1) cnt++;//判断第i位是否为1
        i++; 
    }
    cout<<cnt;
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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