Bitwise AND & short circuit and & difference:
& Both bitwise AND logical operators, & both sides can be Int or boolean expressions. When both sides are int, the number on both sides of the operator must be converted to the binary number before calculation. The short circuit and (&) must be a Boolean expression. Example:
- What is the value of 12 & 5? A: 12 is converted to 1100 (the first four digits are omitted). 5 is converted to 0101, and the result is 0100 (that is, 4). This is the value on both sides;
- If int I = 2, j = 4; (++ I = 2) & (J ++ = 4), the result is false. The process is as follows: first, judge whether ++ I = 2 is true. Of course, this is not true (3 = 2), but the program will continue to judge whether the next expression is true, J ++ = 4, this expression is true, but the & operator requires that the values on both sides of the operator are true, and the result is true, so (++ I = 2) & (J ++ = 4) the result is false. Note: The true condition is that the expressions on both sides are true, but even if we determine that the value of the expression on the left is false, the program still needs to execute to determine whether the expression value on the right is true or false.
- If int I = 2, j = 4; (++ I = 2) & (J ++ = 4), the result is false, the process is basically the same as the above. However, if the value of the expression on the left is false, the program will not continue to judge whether the expression on the right is true or false, this is probably the meaning of short circuit.
Difference between bitwise OR (|) and short circuit or (|:
- The difference between bitwise OR short circuit or is roughly the same as that between (&) and (&).
- (|) And (|) are valid only when one expression is true and the result is true. During (|) operations, no matter whether the value of the expression on the left is true or false, both determine the true and false values of the expression on the right, while (|) in the operation, as long as the value of the expression on the left is true, the value of the expression on the right is no longer judged.