Sword refers to offer to brush questions-GZ11-the number of 1 in binary

Title description:
Input an integer, and output the number of 1s in the 32-bit binary representation of the number. The negative number is represented by one's complement.

Example 1:
input

10

return value

2

If you don’t know about shifting left and shifting right, let’s get to know it for a long time: https://blog.csdn.net/kaixuansui/article/details/92806813
First look at a wrong solution:

public class Solution {
    
    
    //从n的2进制形式的最右边开始判断是不是1
    /*
    * 该解法如果输入时负数会陷入死循环,因为负数右移时,在最高位补得是1
    * 本题最终目的是求1的个数,那么会有无数个1了。
    */
    //-------可能陷入死循环的解法---------------------
    public static int NumberOf1_CanNotUse(int n) {
    
    
        int count = 0;
        while (n != 0) {
    
    
            /*
            * 用1和n进行位与运算,
            * 结果要是为1则n的2进制形式
            * 最右边那位肯定是1,否则为0
            */
            if ((n & 1) == 1) {
    
    
                count++;
            }
            //把n的2进制形式往右推一位
            n = n >> 1;
        }
        return count;
    }
    public static void main(String[] args) {
    
    
        //使用n=10,二进制形式为1010,则1的个数为2;
        int n = -10;
        System.out.println(n + "的二进制中1的个数:" + NumberOf1(n));
    }
}

Next is the positive solution:

public class Solution {
    
    
    //---------------正解--------------------------------
    //思想:用1(1自身左移运算,其实后来就不是1了)和n的每位进行位与,来判断1的个数
    private static int NumberOf1_low(int n) {
    
    
        int count = 0;
        int flag = 1;
        while (flag != 0) {
    
    
            if ((n & flag) != 0) {
    
    
                count++;
            }
            flag = flag << 1;
        }
        return count;
    }
    public static void main(String[] args) {
    
    
        //使用n=10,二进制形式为1010,则1的个数为2;
        int n = -10;
        System.out.println(n + "的二进制中1的个数:" + NumberOf1(n));
    }
}

Next is the optimal solution: the
idea of ​​solving the problem:
if an integer is not 0, then at least one of the integers is 1. If we subtract 1 from this integer, then the 1 that was originally on the far right of the integer will become 0, and all the 0s after the 1 will become 1 (if there are 0s behind the right of 1). All other bits will not be affected.

For example: a binary number 1100, the third digit from the right is a 1 on the far right. After subtracting 1, the third bit becomes 0, the two 0s behind it become 1, and the previous 1 remains unchanged, so the result is 1011. We find that the result of subtracting 1 is the rightmost one All bits starting with 1 are inverted. At this time, if we do the AND operation between the original integer and the result after subtracting 1, all bits from the rightmost 1 of the original integer will become 0. For example, 1100&1011=1000. That is to say, subtracting 1 from an integer, and doing the AND operation with the original integer, will change the rightmost 1 of the integer to 0. Then there are as many 1s in the binary of an integer as possible. Such operations.

public class Solution {
    
    
    public int NumberOf1(int n) {
    
    
        int count = 0;
        while(n != 0){
    
    
            n = n&(n-1);
            count++;
        }
        return count;
    }
      public static void main(String[] args) {
    
    
        //使用n=10,二进制形式为1010,则1的个数为2;
        int n = -10;
        System.out.println(n + "的二进制中1的个数:" + NumberOf1(n));
}

Guess you like

Origin blog.csdn.net/weixin_42118981/article/details/112689579