Java_ Study the next day (v) [Characteristics of bitwise XOR OR operator]

Source: Internet
Author: User

    • "^" Bitwise logical operators

Class YSF {
public static void Main (string[] args) {
System.out.println (5 ^ 10 ^ 10);
System.out.println (5 ^ 10 ^ 5);
"^" Features: one data to another data bit XOR or two times, the number itself is unchanged
}
}

    • Need to define third-party variables


Class YSF {
public static void Main (string[] args) {
int x = 10;
int y = 5;
int temp;
temp = x;
x = y;
y = temp;
System.out.println ("x =" + x + ", y =" + y ");
Define third-party variables, variable swaps
}
}

    • No third-party variables required


Class YSF {
public static void Main (string[] args) {
int x = 10;
int y = 5;
x = x + y; 10 + 5 = 15
y = x-y; 15-5 = 10
x = XY; 15-10 = 5
System.out.println ("x =" + x + ", y =" + y ");
There is no need to define a third-party variable, there is a disadvantage, it is possible to exceed the value range of int
}
}

    • You do not need to define a third-party variable by ^


Class YSF {
public static void Main (string[] args) {
int x = 10;
int y = 5;
x = x ^ y; 10 ^ 5
y = x ^ y; Ten ^ 5 y = 10
x = x ^ y; Ten ^ 5 ^ ten x=5
System.out.println ("x =" + x + ", y =" + y ");
Left x y x right all x ^ y
}
}

Java_ Study the next day (v) [Characteristics of bitwise XOR OR operator]

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.