题目:http://www.nowcoder.com/practice/8ee967e43c2c4ec193b040ea7fbb10b8
C++
负数需要特殊处理,因为负数右移会补1(符号位)
class Solution {
public:
int NumberOf1(int n) {
unsigned int v = n;
int sum = ;
while (v) {
sum += v&;
v >>= ;
}
return sum;
}
};
2024-02-08 09:53:10
题目:http://www.nowcoder.com/practice/8ee967e43c2c4ec193b040ea7fbb10b8
C++
负数需要特殊处理,因为负数右移会补1(符号位)
class Solution {
public:
int NumberOf1(int n) {
unsigned int v = n;
int sum = ;
while (v) {
sum += v&;
v >>= ;
}
return sum;
}
};