Java logical operators and bitwise operators detailed __java programming experience

Source: Internet
Author: User
A. Logical operatorThere are three logical operators, that is, "not", "and", "or", respectively, "!", "&&", "| |" Said. Where: Non-op (!) Indicates negation, such as:!true equals false,!false equals True,!2 equals false,!0 equals True. Returns true if all two conditions before and after the operation (&&) are true, or false. Or Operation (| |) Returns false when the two conditions have one true, and the return is false.
public class data10{
public static void Main (string[] args) {
int a=10;
int b=21;
int c=10;
System.out.println ("Say A>b, right.") "+! (a>b));
System.out.println ("Think of both a>b and A<b, right?") "+ ((a>b) && (a<b));"
System.out.println ("Think of both a>b and A<b, right?") "+ ((a>=b) | | (a==b)));
System.out.println ("Think of both a>b and A=c, right?") "+ ((a<b) | | (a==c)));
}
}

Run Result:
Say A>b, right. True
Think it's both a>b and A<b, right? False
Think it's both a>b and A<b, right? False
Think it's both a>b and A=c, right? True two. Bitwise operatorsBitwise operators are mainly for binary, it includes: "and", "not", "or", "XOR", respectively, "&", "~", "| "," ^ ". Where: With the operation (&) Two operands of the bit is 1, the result is 1, otherwise the result is 0. The bitwise of the operand of the non operation (~) is 0, the result is 1, and if 1, the result is 0. or Operation (|) A bit of two operands as long as there is a 1, the result is 1, otherwise it is 0. The phase of the two operands of the XOR or operation (^) results in the same time as 0, and the result is 1.
public class data13{
public static void Main (string[] args) {
int a=129;
int b=128;
int x=15;
int y=2;
System.out.println ("A and B with the result is:" + (a&b));
System.out.println ("A and B" or the result is: "+ (a|b));"
System.out.println ("x and y) are different or the result is:" + (x^y));
}
}

Run Result:
The results of A and B are: 128
A and B or the result is: 129
X and Y are different or the results are: 13

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.