タイトル説明
内進数1の出力の数を示す整数。前記負の数は補数を示します。
コード
class Solution {
public:
int NumberOf1(int n) {
if(n==0) return 0;
// 每次清除一位的0
int cnt=0;
while(n) {
n = n&(n-1);
cnt++;
}
return cnt;
}
};
0となり、動作位置0への直接アクセスにマイナス1は、元のデータが1になります。