Java bit Operations

Source: Internet
Author: User
Tags bitwise bitwise operators


The bit action designations in Java include:
~ Bitwise NON (NOT)
& Bitwise WITH (and)
| bitwise OR (OR)
^ Bitwise exclusive OR (XOR)
>> Move Right
>>> No sign right shift
<< Move Left
The first few are very simple, mainly the shift operation is more error prone.
The first thing to figure out is the number of digits that are involved in the operation, such as int, which is 32 bits. Long is 64 digits.
such as int i = 1;
The binary original code of I is expressed as:
00000000000000000000000000000001
Long L = 1;
The binary original code of L is expressed as:
0000000000000000000000000000000000000000000000000000000000000001
Two
Positive numbers do not have the inverse code, the complement, also can say the positive number of the inverse code, the complement is the same as the original code.
The inverse of a negative number is the original code is a bit counter,
such as int i =-1;
10000000000000000000000000000001, the highest bit is the sign bit. A positive number is 0 and a negative number is 1.
After-by-bit counter:
01111111111111111111111111111110 is the inverse code.
Counter code plus 1:
01111111111111111111111111111111 is the complement.
Negative numbers are used to participate in the operation of the complement. Get is also the complement, need to subtract 1 to get the original code.
The commonly used bitwise operators are--0 in the in-situ operation.
^ xor or. The same is 0, the difference is 1, any number and 0 are equal to the original value.
& and. All 1 is 1, 0 is 0; any number and 0 are different or equal to 0.
| Or.  1 is 1, all 0 is 0. Any number and 0 are equal to the original value.
<< move left. Complement 0.
>> move right. The sign bit is 0 complement 0, is 1 complement 1.
>>> unsigned right shift. Complement 0.
~ Non-bitwise REVERSE
Four, negative numbers involved in the operation, get the complement, need to be the complement first minus 1, and then take the reverse, get the original code. That is the result of the operation.
0 exceptions, if you get 0, you do not need to subtract 1 and reverse.
In addition, two positive numbers after the calculation is the original code, do not need to subtract 1 and reverse.
Example:
1^-1,
-1
10000000000000000000000000000001--Original Code
01111111111111111111111111111110--Anti-code
01111111111111111111111111111111--complement
1
00000000000000000000000000000001--Original Code
Then 1^-1 equals
01111111111111111111111111111111^
00000000000000000000000000000001=
01111111111111111111111111111110--complement
01111111111111111111111111111101--Anti-code
10000000000000000000000000000010--Original Code ==-2
namely 1^-1=-2
Example:
1^-2
-2
10000000000000000000000000000010--Original Code
01111111111111111111111111111101--Anti-code
01111111111111111111111111111110--complement
1
00000000000000000000000000000001--Original Code
Then 1^-2 equals
01111111111111111111111111111110^
00000000000000000000000000000001=
01111111111111111111111111111111--complement
01111111111111111111111111111110--Anti-code
10000000000000000000000000000001--Original Code ==-1
1.<<
Logic left, 0 on the right, sign bit and other bits.
Positive:
X<<1 is generally equivalent to 2x, but may overflow.
Overflow range: 230~ (231-1) binary means 010000...000 to 01111 .... 1111, after the shift the highest to become 1, into a negative.
Negative:
X<<1 is also equivalent to 2x, it is possible to overflow. So, </

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.