1 class Solution 
 2 {
 3 public:
 4     int hammingWeight(uint32_t n) 
 5     {
 6         int count=0;
 7         while(n)
 8         {
 9             count+=n&1;
10             n>>=1;
11         }
12         return count;
13     }
14 };

把末尾的位摘下来直接累加就行,直到原数字因为右移操作变为0。

posted on 2018-05-22 11:44  高数考了59  阅读(96)  评论(0)    收藏  举报