Java bit operations include: Shift Left (<<), right Shift (>>), unsigned Right shift (>>>), Bit and (&), bit or (|), bit non (~), bit xor (^), except bit non (~) is unary operator, other is a two-dollar operator.
Logical operators &, &&, |, | | :
First, logic & and short-circuit && difference
- In general the difference is the body now, only the left of these two operators is false when there will be a difference, see the following code
1. Logic & Operations
true;boolean b = false;int i = 10;if(b&(i++)>0) System.out.print(i); //输出11,即&的右边有进行运算else System.out.print(i);
2. Short-circuit && operation
true;boolean b = false;int i = 10;if(b&&(i1++)>0) System.out.print(i); //输出10,即&&的右边没有运算else System.out.print(i);
Summary:
&: Whether true or false on the left of &, the right side will be calculated
&&: As long as the left is false, the right side will not be calculated
The && is selected in half, because it can improve efficiency and exception handling, and can also be skipped when the right side produces an exception.
Second, logic | and short-circuit | | The difference
- Similar to the above, only the two are only when the left is true, there will be a difference, see the following code
1. Logic | of the operation
true;int i = 10;if(a|(i++)>0) System.out.print(i); //输出11,即的右边有进行运算else System.out.print(i);
2. Short-circuit | | The operation
true;int i = 10;if(a||(i++)>0) System.out.print(i); //输出10,即||的右边没有运算else System.out.print(i);
Summary:
| : When the left is true, the same operation is performed on the right
|| : When the left is true, no operations are performed on the right
Java bitwise operations (SHIFT, bit and, or, XOR, non) and logical operations