Bitwise operations in Java--java Programming thought notes

Source: Internet
Author: User
Tags bitwise bitwise operators

Welcome reprint, Reproduced Please be sure to indicate the source: http://blog.csdn.net/alading2009/article/details/40450421



There are four bitwise operators in Java :& (Bitwise AND), | (bitwise OR), ^ (bitwise XOR), ~ (bitwise non).

1, first look at the bitwise AND (&)

public class Main {public    static void Main (string[] args) {        //bitwise with, 21 to 1, 0 to 0        System.out.println ( Integer.tobinarystring ());        System.out.println (Integer.tobinarystring (one));        System.out.println (integer.tobinarystring (13&11));}            }
The result is:

110110111001

From the results can be clearly seen, with the rule is: two-bit phase, if one is 0, then the result is 0, otherwise 1.






2, then the bitwise OR (|)

<span style= "FONT-SIZE:18PX;" >public class Main {public    static void Main (string[] args) {        //bitwise OR, 20 to 0, 1 to 1        System.out.println ( Integer.tobinarystring ());        System.out.println (Integer.tobinarystring (one));        System.out.println (integer.tobinarystring (13|11));}    } </span>
The result is:

110110111111
So you get a bitwise OR result.






3. Next is the bitwise XOR or

public class Main {public    static void Main (string[] args) {        //Bitwise XOR: Two-bit different 1, two-bit same 0        System.out.println ( Integer.tobinarystring ());        System.out.println (Integer.tobinarystring (one));        System.out.println ("0" +integer.tobinarystring (13^11));}    }

The result is:

110110110110
Different 1, same 0.


Bitwise XOR or has an interesting usage, it can exchange two number of values without a third-party variable, as follows

public class Main {public    static void Main (string[] args) {        //Use bitwise XOR or Exchange two-number value        int temp1=10;        int temp2=114;        System.out.println ("Before Exchange: Temp1:" +TEMP1);        System.out.println ("Before Exchange: Temp2:" +TEMP2);        TEMP1^=TEMP2;        TEMP2^=TEMP1;        TEMP1^=TEMP2;        SYSTEM.OUT.PRINTLN ("After Exchange: Temp1:" +TEMP1);        SYSTEM.OUT.PRINTLN ("After Exchange: Temp2:" +TEMP2);}    }
The result is:

Before: Temp1:10 swap: temp2:114 after Exchange: temp1:114 after Exchange: Temp2:10
There is a basis for this, so to see,

The first step: TEMP1^=TEMP2, that is, TEMP1=TEMP1^TEMP2

Step Two: temp2=temp2^temp1=temp2^ (TEMP1^TEMP2), XOR or satisfy the commutative law, after the parentheses are finally obtained TEMP2=TEMP1

Step three: Temp1=temp1^temp2= (TEMP1^TEMP2) ^TEMP2=TEMP1^TEMP2^TEMP1=TEMP2

After these three steps, the values of the two variables are exchanged smoothly.

This method tells us that the swap function can be implemented in C + +

    int swap (int &a, int &b) {        a^=b;        B^=a;        a^=b;    }
The use of the reference, of course, you can also use a pointer to the way to achieve




4, the last is the bitwise NON (~)

public class Main {public    static void Main (string[] args) {        //bitwise NON: Bitwise de-        System.out.println (" 0000000000000000000000000000 "+integer.tobinarystring ());        System.out.println (integer.tobinarystring (~13));}    }
The result is:

0000000000000000000000000000110111111111111111111111111111110010




Welcome reprint, Reproduced Please be sure to indicate the source: http://blog.csdn.net/alading2009/article/details/40450421





Bitwise operations in Java--java Programming thought notes

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.