# [leetcode] 191. 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.

Solution

Idea: convert a unsigned integer into binary number

class Solution {
public:
int hammingWeight(uint32_t n) {
int ret = 0;
while (n!=0){
ret += n%2;
n = n/2;
}
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 解题报告

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

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)