Those annoying bits of Java operations (&, | ...)

& & & And the same point:

Means "and" operation. Here the "and" and mathematics "and or" the same "and" meaning, follow "a false must be false" principle. I.e., "and" both sides of the symbols of elements as long as there is a false, "and" execution result of the operation is false.

& And && difference:

1)
& indicates "bitwise", where "bit" refers to a binary bits (bit).
Example: 8 decimal number is converted to binary: 1000; 9 into a binary number is 1001.
If the following procedures:

public class Test {
public static void main(String[] args) {
System.out.println(9 & 8);
	}
}

The output should be: 8
Cause: 1001 & 1000 = 1000. General computer 1 for true, 0 for false. Leftmost bit 1 & 1 = 1, the rightmost one 1 & 0 = 0.

  1. && represents logical "and", i.e., a boolean value of java can be present in the left and right sides of && symbols.
    true && false = false, true && true = true, is still "a false must be false."
    Note that the values are: && sign the so-called "short circuit principle", when the A && B occurs, judged to be false if A, then B expression would not get the opportunity to perform or judge. A direct result of it is false.
    Note: For decimal and binary conversion, each of four can simply follow the "8421" principle, i.e., 1001 = 9,1011 i.e. 1 + 8 8 + 2 + 1 = 11

Java中的>> << >>>

<<  表示左移,不分正负数,低位补0
>>  表示右移,如果该数为正,则高位补0,若为负数,则高位补1
>>> 表示无符号右移,也叫逻辑右移,即若该数为正,则高位补0,而若该数为负数,则右移后高位同样补0
System.out.println("16 << 1 : " + (16 << 1));
System.out.println("16 >> 3 : " + (16 >> 3));
System.out.println("16 >> 10 : " + (16 >> 10));
System.out.println("1 >> 1 : " + (1 >> 1));
System.out.println("16 >>> 2 : " + (16 >>> 2));
System.out.println("-16 >> 2 : " + (-16 >> 2));
System.out.println("-16 << 2 : " + (-16 << 2));
System.out.println("-16 >>> 2 : " + (-16 >>> 2));

Print results

16 << 1 : 32
16 >> 3 : 2
16 >> 10 : 0
1 >> 1 : 0
16 >>> 2 : 4
-16 >> 2 : -4
-16 << 2 : -64
-16 >>> 2 : 1073741820

Simple to understand:

<< 1  相当于乘以2

>> 1  相当于除以2

>>> 不考虑高位的正负号,正数的 >>> 等同于 >>
 

|=

About | = Operator: | = + = operator and operators such as this, is to dismantle a = a | b;

code show as below:

public static strictfp void main(String[] args) {

   int a = 5; // 0000 0101
   int b = 3; // 0000 0011
   a |= b; // 0000 00111
   System.out.println(a);

}
 

Specific rules: two binary bits corresponding to the bit is 0, 0, and 1 otherwise. Take 5 binary 00000101 and 000000113 is binary | operation, the corresponding bit of the three bits are not simultaneously equal to 0, so the final result is binary 00000111 7.

&=

And & = | = basically the same, but in different ways for comparing parity.

code show as below:

 
public static strictfp void main(String[] args) {

    int a = 5; // 0000 0101
    int b = 3; // 0000 0011
    a &= b; // 0000 0001
    System.out.println(a);
}
 
 

Specific rules: two binary bits correspond to 1, the result is 1, otherwise the result is all 0. Take 5 binary 00,000,101 and 000,000,113 are binary & operator, only the last digit is 1, then the final result is a binary 0000 0001 1.

^=

Rules of operation or above.

code show as below:

public static strictfp void main(String[] args) {
    int a = 5; // 0000 0101
    int b = 3; // 0000 0011
    a ^= b; // 0000 0110
    System.out.println(a);
}

Specific rules: two binary phase corresponding to the same time, the result is 0, otherwise the result is 1. Take 5 binary and binary 00000101 000000113 ^ calculation is performed, the corresponding bits are 0 bits 1-5 so the 1-5 bits are 0, the 8th bit is 1 so the 8th bit is also 0, the other Therefore, the corresponding bit is not equal 1, then the final result is 00000110 binary is 6.

Released 1258 original articles · won praise 10000 + · views 840 000 +

Guess you like

Origin blog.csdn.net/a1439775520/article/details/104430456