題目描述
輸入一個整數,輸出該數二進制表示中1的個數。其中負數用補碼錶示。
class Solution {
public:
int NumberOf1(int n) {
int count = 0;
while(n != 0) {
count++;
n = n & (n - 1);
}
return count;
}
};
class Solution {
public:
int NumberOf1(int n) {
int count = 0;
while(n != 0) {
count++;
n = n & (n - 1);
}
return count;
}
};