【leetcode 位运算 C++】191. Number of 1 Bits

191. Number of 1 Bits

在这里插入图片描述

class Solution {
    
    
public:
    int hammingWeight(uint32_t n) {
    
    
        int cnt = 0;
        for(uint32_t bit = 1; bit; bit <<= 1) if(n & bit) cnt++;
        return cnt;
    }
};

猜你喜欢

转载自blog.csdn.net/m0_37454852/article/details/113936261