Java operator with (&), non (~), or (|), XOR (^)

Source: Internet
Author: User
Tags arithmetic bitwise

1. Bitwise XOR or operation (^)

The arithmetic rules are: two numbers are converted to binary, then compared from high, if the same is 0, not the same is 1.

For example: 8^11.

8 to binary is 1000,11 to binary is 1011. The comparison from the high level is: 0011. Then the binary is converted to decimal, which is Integer.parseint ("0011", 2) = 3;

2. Bitwise AND operator (&)

Operation rules: Two numbers are converted to binary, then from the high start comparison, if the two number is 1 is 1, otherwise 0.

For example: 129&128.

129 conversion to binary is 10000001,128 to binary is 10000000. From a high level to get a comparison, get 10000000, that is, 128.

3. Bitwise OR operator (|)

Operation rules: Two numbers are converted to binary, and then from the high start comparison, two numbers as long as there is a 1 is 1, otherwise it is 0.

For example: 129|128.

129 conversion to binary is 10000001,128 to binary is 10000000. From a high level to get a comparison, get 10000001, that is, 129.

4. Bitwise non-operator (~)

Arithmetic rule: If the bit is 0, the result is 1, if the bit is 1, the result is 0.

For example: ~37

In Java, all data is represented in the form of a complement, and if there is no special description, the data type in Java is by default the length of the Int,int data type is 8 bits, one is four bytes, or 32 bytes, 32bit.

8 conversion to binary is 100101.

After the complement: 00000000 00000000 00000000 00100101

Reverse: 11111111 11111111 11111111 11011010

Because the high is 1, so the original code is negative, the complement of the negative is its absolute value of the original code to take the reverse, and add 1 at the end.

Therefore, we can restore the complement of this binary number: first, the end minus 1 is the inverse code: 11111111 11111111 11111111 11011001 Secondly, we will take the original code:

00000000 00000000 00000000 00100110, at this point the binary transcoding is 38

So ~37 =-38.

Java operator with (&), non (~), or (|), XOR (^)

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.