Logic in Java with, logical OR, bitwise-AND, bitwise-OR-differentiated

Source: Internet
Author: User

All along, the concept of such a very vague, today summed up a bit

Start with logic and (&&), logic or (| |)
They're short-circuiting, for example.
int i = 0;
System.out.println (i++ = = 1 && i++ = = 2);//Print False
System.out.println (i);//print 1
Here first to determine whether i++ is equal to 1, because it is the right self-increment, so here i++ = = 1 is wrong, short-circuit means not to perform the back i++ = = 2,
Directly returns a false, so that's why the final result prints 1.
i = 0;//here re-assigned, for the following example to understand
System.out.println (++i = = 1 && i++ = = 2);//Print False
System.out.println (i);//Print 2
And here is the opposite, ++i is equal to 1, so it executes the back i++ = = 2, the final value of I will print 2
Logical and is when both sides of the operation is true, return true, otherwise false, if the left return False, will return false, no longer continue to execute the code on the right
Logic returns True if the left side is true, no longer executes the code on the right, but returns the result of the right operation if the left value is False

Bitwise AND & Bitwise OR |

They are the same as logic, logic or the same, the difference is the non-short-circuit operation, that is, the & operator, even if the left is false, but also to execute the right code; | Even if the left is true, execute the code on the right
There is also a point to point out that the difference is
Logical AND, logical, or in Java, the operator must be of type Boolean, while bitwise AND, bitwise, or can be of type int,
Let me give you an example of how they work.

System.out.println (5&3);//(code 1) Printing Results 1
System.out.println (5|3);//(code 2) printing Results 7
System.out.println (4&2);//(code 3) printing results 0
System.out.println (4|2);//(code 4) printing Results 6

The binary code is as follows:
5 101 4 100
3 011 2 010
& 001 000
//| 110 110

Bitwise-AND-operation:

See note 3 for Comments 1 and 2, you will find that except two bits are 1, the rest of the & results are 0

bitwise OR of the operation:

The result is 0 when all two numbers are 0, otherwise the result is 1

Logic in Java with, logical OR, bitwise-AND, bitwise-OR-differentiated

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.