【位运算】位1的个数

题目:

 

 

解答:

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

 

posted @ 2020-05-16 14:00  梦醒潇湘  阅读(272)  评论(0)    收藏  举报