位1的个数 (Number of 1 Bits)

 

思路:»> n&1=1

// @Title: 位1的个数 (Number of 1 Bits)
// @Author: qisiii
// @Date: 2022-03-10 21:15:28
// @Runtime: 0 ms
// @Memory: 38.6 MB
// @comment: >>>  n&1=1
// @flag: RED
public class Solution {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
        int count=0;
        while(n!=0){
            if((n&1)==1){
                count++;
            }
            n=n>>>1;
        }
        return count;
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18