leetcode 剑指 Offer 15. 二进制中1的个数 简单

n & (n - 1) = 除去 n 二进制中的最后一个 1
class Solution { public: int hammingWeight(uint32_t n) { int cnt = 0; while(n) { ++ cnt; n &= (n - 1); } return cnt; } };

n & (n - 1) = 除去 n 二进制中的最后一个 1
class Solution { public: int hammingWeight(uint32_t n) { int cnt = 0; while(n) { ++ cnt; n &= (n - 1); } return cnt; } };