Java bitwise operations (SHIFT, bit and, or, XOR, non) and logical operations

Source: Internet
Author: User

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

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.