Today's Niuke == Number of 1s in Binary == Original Complement Binary Shift Operation

Source: Niuke https://www.nowcoder.com/practice/8ee967e43c2c4ec193b040ea7fbb10b8?tpId=188&&tqId=36702&rp=1&ru=/ta/job-code-high-week&qru=/ta/job-code-high-week/question- ranking
statement: If I violate anyone's rights, please contact me and I will delete it.
Welcome experts to spray me

Number of 1s in the title 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 complement.
Enter 10 and
return value 2

== The examples here are not complete, missing negative numbers ==
Examples I added myself:

Input-1 output 32
input-2147483648 output 1

Great God's code:

Just one sentence: That is, n&(n-1) will eliminate the rightmost 1 of n (that is, the first 1 from right to left). In the loop, eliminate a 1 of n each time, and exit if there is no 1 when n==0

For example:
a binary number n=1100.
After subtracting 1 from n, n-1=1011, the third bit becomes 0, the two 0s behind it become 1, and the first 1 remains unchanged.
We find that the result of subtracting 1 is to change the rightmost one All bits starting with 1 are inverted.
Do n&(n-1) again, and all the bits from the rightmost 1 of the original integer will become 0. For example, 1100&1011=1000. In
other words, 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 int NumberOf1(int n) {
    
      
      int ans = 0;
        while (n != 0) {
    
    
            ++ans;
            n = n & (n-1);
        }
        return ans;
    }
The general idea is to calculate whether each bit is 1

What is used here is >>> unsigned right shift, that is, whether it is positive or negative, it is filled with 0

    public int NumberOf1(int n) {
    
    
        int count = 0;
        while(n != 0){
    
    
            count += (n & 1); //每次判断最低位是否为1
            n >>>= 1;
        }
        return count;
    }
Slightly talk about this and that of java binary
Original code and complement
  • The binary of a positive number is the original code, ie 3 = 011
  • The binary of the negative number is the complement code, that is, the sign bit remains unchanged, and the original code is inverted and added 1 to obtain the complement code, ie -3 = 1 1101
  • String string = Integer.toBinaryString(n); Use this operation in java to get the binary representation of n:
  • As shown in the figure below, the leftmost 0 is omitted here- Insert picture description here
Binary shift >>,<<,>>>

Here is the shift operation, let’s take a look at the binary shift operation >>,<<,>>>,<<< (the key isNegative shiftIt’s a bit special) because the sign bit of a negative number is on the far left, it’s a bit...

  • << shift to the left, the end of the binary is directly filled with 0, there is no difference between positive and negative, because here are all filled with 0, and because the sign bit of the negative number is on the leftmost
  • >> Shift to the right, there are two cases: the beginning is completed with the sign bit
    • Negative numbers are complemented by 1, for example, the binary value of -5 is 1111…1011, shift two bits to the right 11 1111…10
    • Start with 0 to complete the positive number
  • >>> is an unsigned shift operation, whether it is right-filled or left-filled, use 0
  • <<< There is no such operation, Why, there is no difference between unsigned right shift and <<

int n = 5;
int m = -5;
System.out.println(“n的二进制:”+Integer.toBinaryString(n));
System.out.println(“m的二进制:”+Integer.toBinaryString(m));
System.out.println("n>>2: "+Integer.toBinaryString((n>>2)));
System.out.println("m>>2: "+Integer.toBinaryString((m>>2)));
System.out.println("n<<2: "+Integer.toBinaryString((n<<2)));
System.out.println("m<<2: "+Integer.toBinaryString((m<<2)));
System.out.println("n>>>2: "+Integer.toBinaryString((n>>>2)));
System.out.println("m>>>2: "+Integer.toBinaryString((m>>>2)));
Insert picture description here

Precedence with binary operators

Insert picture description here

Reference blog

Complement and inverse code in Java:
https://blog.csdn.net/weixin_37870009/article/details/79775926?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1.control&depth_1-utm_source=distribute.pc_relevant. none-task-blog-BlogCommendFromBaidu-1.control

Java original code, inverse code, complement calculation and inversion (~) operation:
https://blog.csdn.net/u010841296/article/details/52850307

Summary of operators operating binary in java (&, |, ^, ~, <<,>>, >>>):
https://blog.csdn.net/xinghuo0007/article/details/78453442

Summarize the bit operations in java and the priority of operations:
https://blog.csdn.net/sadjladjlas/article/details/51192106

Java study notes-get the original code of positive numbers and the complement of negative numbers:
https://blog.csdn.net/u010503427/article/details/79631121

Guess you like

Origin blog.csdn.net/qq_45531729/article/details/110949414