# 题目

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.

# 代码

class Solution {
public:
int hammingWeight(uint32_t n) {
int ret = 0;
while(n != 0)
{
++ret;
n &= (n-1); //抹掉最右边的1
}//while
return ret;
}
};

## LeetCode 191. Number of 1 Bits

Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know...

## Leetcode 191 Number of 1 Bits

Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know...

## LeetCode 191.Number of 1 Bits 解题报告

LeetCode 191.Number of 1 Bits 解题报告

## [191] Number of 1 Bits

• camlot_
• 2016年06月27日 20:23
• 193

## 148. Sort List\191. Number of 1 Bits\476. Number Complement\342. Power of Four

举报原因： 您举报文章：LeetCode（191）Number of 1 Bits 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)