[leetcode] Number of 1 Bits

Number of 1 Bits

Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).

For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3.
 
 
 1 class Solution
 2 {
 3 public:
 4   int hammingWeight(uint32_t n)
 5   {
 6     int count = 0;
 7     while(n > 0)
 8     {
 9       if(n % 2 != 0)
10         count++;
11       n /= 2;
12     }
13 
14     return count;
15   }
16 };
原文地址:https://www.cnblogs.com/lxd2502/p/4325483.html