面试题15. 二进制中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-09 13:56  梦醒潇湘  阅读(105)  评论(0)    收藏  举报