Java by bit and (&) and short circuit and (&) by bit or (|) and short circuit or (|) Difference Summary

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.