Some blogs refer to bitwise & as non-short-circuit logical AND, and bitwise| as non-short-circuit logical OR. In fact, it is only equivalent in effect. It is the clever operation of bitwise & and bitwise|, not java itself. such an operation,
true & true ===" true
true & false ===> false
FALSE & TRUE ===> FALSE
FALSE & FALSE ===> FALSE
From the effect, it can be found that the bitwise & can be called a non-short-circuit logical AND. Regardless of the result of the first judgment expression, the second judgment expression will be evaluated, and the results of the two judgment expressions before and after the bitwise & operation The behavior after is consistent with the logical &&.
true | true ===" true
true | false ===" true
false | true ===" true
false | false ===> false
From the effect, it can be found that the bitwise | can be called a non-short-circuit logical OR. Regardless of the result of the first judgment expression, the second judgment expression will be evaluated, and the results of the two judgment expressions before and after the bitwise | operation The behavior after is consistent with the logical|.
In Netty, the MathUtil class has a function to determine whether the array is out of bounds. The code is as follows:
public static boolean isOutOfBounds(int index, int length, int capacity) { return (index | length | (index + length) | (capacity - (index + length))) < 0; // As long as one of these values is negative, the result of the overall OR operation is negative, that is, it returns true; if both are positive, the result of the overall OR operation is positive, and it returns false. }