Binary number of questions surface 15

& Itself (themselves -1) can make the lowest bit 1 is set to 0

 Java binary complement with the negative type int stored

The following algorithm is not suitable for negative

    public int numberOf1(int num) {
        int count = 0;
        while (num != 0) {
            num = num & (num - 1);
            count++;
        }
        return count;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int a = 60;
        System.out.println(solution.numberOf1(a));
    }

 

Guess you like

Origin www.cnblogs.com/youzoulalala/p/11300249.html