Leetcode 191. Number of 1 Bits | | Leetcode 191. Number of 1 Bits 文章作者:Tyan博客:noahsnail.com | CSDN | 简书 1. Description 2. Solution Version 1 123456789101112class Solution {public: int hammingWeight(uint32_t n) { int count = 0; int m = 1; while(n) { count += (m & n); n >>= 1; } return count; }}; Version 2 123456789101112class Solution {public: int hammingWeight(uint32_t n) { int count = 0; int m = 1; while(n) { count++; n &= (n - 1); } return count; }}; Reference https://leetcode.com/problems/number-of-1-bits/description/ 如果有收获,可以请我喝杯咖啡! 赏 微信打赏 支付宝打赏