HJ15题解 | #求int型正整数在内存中存储时1的个数#

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

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

#include <iostream>
#include <vector>
using namespace std;

int main() {
    int input = 0;
    cin >> input;
    vector<int> vec;
    for(int i = 31; i >= 0; i--) {
        vec.push_back(((input >> i) & 1)); //右移 并与1
    }
    int count = 0;
    for(auto it : vec) { //遍历求和1的个数
        if (it == 1) {
            count++;
        }
    }
    cout << count;
}
// 64 位输出请用 printf("%lld")

#刷题记录#
全部评论

相关推荐

07-02 10:44
门头沟学院 C++
码农索隆:太实诚了,告诉hr,你能实习至少6个月
点赞 评论 收藏
分享
人力小鱼姐:实习经历没有什么含金量,咖啡店员迎宾这种就别写了,其他两段包装一下 想找人力相关的话,总结一下个人优势,结合校园经历里有相关性的部分,加一段自我评价
点赞 评论 收藏
分享
龙珠传说:nb,公务员解约不需要支付违约金吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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