Detailed explanation of the meaning of ^, &, | and bit operators in JAVA

1. ^ (exclusive OR operator)

^ Is a binary operator for binary. Operation rules: If two binary values ​​are the same in the same bit, the bit in the result is 0, otherwise it is 1, such as 1011 & 0010 = 1001.

2. | (or operator)

| Is a binary operator for binary. Operation rules: If two binary values ​​have at least one 1 in the same bit, the bit in the result is 1, otherwise it is 0, such as 1011 & 0010 = 1011.

3. & (and operator)

& Is a binary operator for binary. It should be noted that && is an identifier representing "and" between judgment conditions in java, & is a binary operator, if two binary values ​​are both 1 in the same bit, the bit in the result is 1, otherwise If it is 0, it can be considered that both are true (1), and the result is also true (1), such as 1011 & 0110 = 0010.

& There is also a more important place, which is also a common problem in interviews, that is, the operator can calculate the remainder. We know that the slowest of the four arithmetic operations is division, and the remainder operation is even slower, so you can use & to quickly find the remainder of two numbers. Let’s look at an example:

public ModTest{
    public static void main(String[] args){
        System.out.println(45 & 11);
        System.out.println(45 & 7);
    }
    /**result:3, 5*/
}

Given two values ​​of x and y, if you want to find the remainder of x and y, you only need x & (y-1). As shown in the above example, if you want to find the remainder of 45 and 12 (45 and 8), you only need 45 & 11 (45 & 7).


The following three operators are bitwise operators

>>x (constant): move x bits to the right (the vertex moves in which direction), if the number is positive, the high bit (leftmost) is filled with x 0, if it is negative, the top bit is filled with x One.

<<x (constant): Move x bits to the left (the vertex moves in which direction), no matter the low bit (rightmost) of the positive or negative number, add x zeros.

<<<: No such representation.

>>>x (constant): Indicates unsigned right shift by x bits. The so-called unsigned is compared with >>x. After this operation, the high bit (leftmost) of the positive or negative number is filled with 0.

4. << (left shift operator)

Example 1: -20<<2

Original code: 10000000 00000000 00000000 00010100

Inverse code: 11111111 11111111 11111111 11101011 (sign bit remains unchanged, other bits are inverted)

Complement code: 11111111 11111111 11111111 11101100 (inverse code + 1)

Shift two digits to the left (add 0 to the two digits on the rightmost side)

Complement: 11111111 11111111 11111111 10110000

Inverse code: 11111111 11111111 11111111 10101111 (complement-1)

Original code: 10000000 00000000 00000000 01010000 (sign bit remains unchanged, other bits are reversed)

Result: -80

Example 2: 20<<2

Original code (inverse code, complement code): 00000000 00000000 00000000 00010100

Shift two digits to the left (add 0 to the two digits on the rightmost side)

Original code (inverse code, complement code): 00000000 00000000 00000000 01010000

Results: 80

5. >> (shift right operator)

Example 1: -20>>2

Original code: 10000000 00000000 00000000 00010100

Inverse code: 11111111 11111111 11111111 11101011 (sign bit remains unchanged, other bits are inverted)

Complement code: 11111111 11111111 11111111 11101100 (inverse code + 1)

Move two bits to the right (add 1 to the two leftmost bits)

Complement: 11111111 11111111 11111111 11111011

Inverse code: 11111111 11111111 11111111 11111010 (complement-1)

Original code: 10000000 00000000 00000000 00000101 (sign bit remains unchanged, other bits are reversed)

Result: -5

Example 2: 20>>2

Original code (inverse code, complement code): 00000000 00000000 00000000 00010100

Move two bits to the right (add 0 to the two leftmost bits)

Original code (inverse code, complement code): 00000000 00000000 00000000 00000101

Result: 5

6. >>> (unsigned right shift operator)

Example 1: -2>>>1

Original code: 10000000 00000000 00000000 00000010

Inverse code: 11111111 11111111 11111111 11111101 (sign bit remains unchanged, other bits are inverted)

Complement code: 11111111 11111111 11111111 11111110 (inverse code + 1)

Shift 1 bit to the right (unsigned bit operator, only add 0 to the leftmost bit)

Complement: 01111111 11111111 11111111 11111111

Inverse code: 01111111 11111111 11111111 11111111 (the high bit is 0, positive number)

Original code: 01111111 11111111 11111111 11111111 (same as inverse code)

Result: 2147483647

Example 2: 2>>>1

Original code (inverse code, complement code): 00000000 00000000 00000000 00000010

Move one bit to the right (add 0 to the leftmost bit)

Original code (inverse code, complement code): 00000000 00000000 00000000 00000001

Result: 1

7. Additional knowledge

^=, |=, &=, <<=, >>=, >>>= are almost the same as without the equal sign, except that the assignment operation is added. Take >>= as an example:

public class Test {
	public static void main(String[] args) {
		int num = 2;
		System.out.println(num>>=1);
        System.out.println(num);
	}
    /**result:1  1

}

Note: Since there is an assignment operation, the left side of the operator cannot be a constant, for example, 2>>=1 will report an error.

The data stored in the calculation is in the form of complement

 

references:

https://www.cnblogs.com/liaopeng/p/8436155.html

http://www.cnblogs.com/chuijingjing/p/9405598.html

https://blog.csdn.net/tianyeshiye/article/details/80261622

Guess you like

Origin blog.csdn.net/weixin_48968045/article/details/112298187