Bitwise operations in Java)

Source: Internet
Author: User

Original Author: Rosen Jiang Source:Http://www.blogjava.net/rosen

Shift Operator

Including:
"> Right shift"; "<left shift"; ">>> unsigned right shift"

Example:
-5> 3 =-1
1111 1111 1111 1111 1111 1111 1111
1111 1111 1111 1111 1111 1111 1111
The result is exactly the same as math. Floor (double)-5/(2*2*2.

-5 <3 =-40
1111 1111 1111 1111 1111 1111 1111
1111 1111 1111 1111 1111 1111 1101
The result is exactly the same as-5*2*2*2.

5> 3 = 0
0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000
The result is exactly the same as 5/(2*2*2.

5 <3 = 40
0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0010
The result is exactly the same as 5*2*2*2.

-5> 3 = 536870911
1111 1111 1111 1111 1111 1111 1111
0001 1111 1111 1111 1111 1111 1111

Whether positive or negative, their right shift, left shift, and unsigned right shift are all themselves, for example,-5 <32 =-5,-5> 32 =-5,-5> 32 =-5.
An interesting phenomenon is that 1 is shifted to 31 places and 31 places to the right. The result is-1.
0000 0000 0000 0000 0000 0000 0000
1000 0000 0000 0000 0000 0000 0000
1111 1111 1111 1111 1111 1111 1111

Bit logical operators

Including:
&; | Or ;~ Non (also called reverse); ^ XOR or

"&", "|", "~ "Non" is a basic logical operation, which can evolve into "Non", "or", "and" or "Compound logical operations. "^ Exclusive or" is a special logical operation. If you reverse it, you can obtain "same or". Therefore, "same or" logic is also called "exclusive or non" logic.

Example:
5 & 3 = 1
0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000

-5 & 3 = 3
1111 1111 1111 1111 1111 1111 1111
0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000

5 | 3 = 7
0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000

-5 | 3 =-5
1111 1111 1111 1111 1111 1111 1111
0000 0000 0000 0000 0000 0000 0000
1111 1111 1111 1111 1111 1111 1111

~ 5 =-6
0000 0000 0000 0000 0000 0000 0000
1111 1111 1111 1111 1111 1111 1111

~ -5 = 4
1111 1111 1111 1111 1111 1111 1111
0000 0000 0000 0000 0000 0000 0000

5 ^ 3 = 6
0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000

-5 ^ 3 =-8
1111 1111 1111 1111 1111 1111 1111
0000 0000 0000 0000 0000 0000 0000
1111 1111 1111 1111 1111 1111 1111

========================================================== ======================================

The bit operations in Java include:

~ Non-(not) by bit)
& Bitwise AND (and)
| Bitwise OR (OR)
^ XOR)
> Right shift
>>> Unsigned right shift
<Move left
The previous steps are very simple, mainly because shift operations are prone to errors.
First, we need to figure out the number of digits involved in the calculation, for example, the int value is 32 bits. Long is 64-bit.
For example, int I = 1;
The binary original code of I is:
00000000000000000000000000000001
Long L = 1;
The binary original code of L is:
0000000000000000000000000000000000000000000000000000000000000001
II,

If positive numbers do not have an anti-code or complement code, you can also say that positive numbers have the same anti-code and complement code as the original code.
The Negative Inverse code is the bitwise inverse of the original code,
For example, int I =-1;
10000000000000000000000000000001, the highest bit is the symbol bit. The positive value is 0, and the negative value is 1.
After bitwise inversion:
01111111111111111111111111111110 is the anti-code.
Anti-code plus 1:
01111111111111111111111111111111 is the completion code.
Negative numbers are all involved in the computation with the complement code. The obtained code is also a complementary code. You need to subtract 1 to obtain the original code.

3. the commonly used bitwise operator-0 is special in bitwise operations.

^ Exclusive or. If the number is the same as 0, the difference is 1. If the number is different from 0, the value is equal to or equal to the original value.
. All 1 is 1, and 0 is 0; any number is different from 0 or equal to 0.
| Or. 1 is 1, and all 0 is 0. Any number is equal to zero or equal to the original value.
> Right shift. The sign bit is 0 to 0, and 1 to 1.
>>> Unsigned right shift. Add 0.
~ Non-bitwise Inversion

4. If a negative number is involved in the operation, the complement code is obtained. You need to first subtract 1 from the complement code and then get the reverse code by bit to obtain the original code. The operation result.

With the exception of 0, if the result is 0, you do not need to subtract 1 or reverse.
In addition, after two positive numbers, the original code is obtained, without the need to subtract 1 or reverse.
Example:
1 ^-1,
-1
10000000000000000000000000000001 -- original code
01111111111111111111111111111110 -- anti-code
01111111111111111111111111111111 -- supplemental code
1
00000000000000000000000000000001 -- original code
1 ^-1 equals
01111111111111111111111111111111 ^
00000000000000000000000000000001 =
01111111111111111111111111111110 -- supplemental code
01111111111111111111111111111101 -- anti-code
10000000000000000000000000000010 -- original code =-2
That is, 1 ^-1 =-2
Example:
1 ^-2
-2
10000000000000000000000000000010 -- original code
01111111111111111111111111111101 -- anti-code
01111111111111111111111111111110 -- supplemental code
1
00000000000000000000000000000001 -- original code
Then 1 ^-2 equals
01111111111111111111111111111110 ^
00000000000000000000000000000001 =
01111111111111111111111111111111 -- supplemental code
01111111111111111111111111111110 -- anti-code
10000000000000000000000000000001 -- original code =-1
1. <
The logic is shifted to the left, and the right side is supplemented with 0. the symbol bit is the same as the other bit.
Positive number:
X <1 is generally equivalent to 2x, but may overflow.
Overflow range: 230 ~ (231-1) binary represents 010000 ...... 000 to 01111 ...... 1111. After the shift, the highest value is 1 and the number is negative.
Negative Number:
X <1 is generally equivalent to 2x, and may also overflow. Therefore, x * 32 can be written as x <5
Overflow range:-231 ~ -(230 + 1) binary representation of 10000... 000 to 101111... 1111. After the shift, the highest value is 0 and the value is positive.
2.>
The right shift of arithmetic. It does not match the above. If it is a positive number, the left side is supplemented by 0. If it is a negative number, the left side is supplemented by 1.
X> 1, which is equivalent to X/2. The remainder is discarded because it is reduced, so it will not overflow.
But pay attention to one thing:-1 shifts the number of places to the right is-1.
In addition, the remainder discarded is positive, and 3> 1 = 1 the remainder discarded is 1.
-3> 1 =-2 The remainder discarded is also 1, rather than-1.
Equal to positive numbers x> 1 and X/2
The numbers x> 1 and X/2 are not necessarily equal.
3. >>>
The logic is shifted to the right, which corresponds to <
This moves the symbol bits together, and adds 0 to the left.
For positive numbers, >>>and >>are the same.
For a negative number, after the right shift, it becomes a positive number.
You can use integer. tobinarystring (int I) to view 01 BITs, which is more intuitive.
Consider the following code:
For (val = 0; Val <100000; Val + = 5) {alterx = Val * 8; myresult = Val * 2 ;}
Replacing multiplication with shift operations can greatly improve performance. The modified code is as follows:
For (val = 0; Val <100000; Val + = 5) {alterx = Val <3; myresult = Val <1 ;}
The modified code does not multiply by 8. Instead, it uses the equivalent three-bit left shift operation, with 1 shifted to 2. Correspondingly, the one-bit operation on the right is equivalent to dividing by 2. It is worth mentioning that, although the shift operation is fast, it may make the code more difficult to understand, so it is best to add some comments.

The unsigned right shift operator ">>>" fills 0 in the leftmost part of the bit string when it shifts the right of the bit string, which is different from the signed right shift operator ">>. ">" Fills in the original leftmost bits from the leftmost of the bit string when the bit string is shifted to the right. That is to say, the leftmost bit of the bit string is the symbol bit. If it is 1, the leftmost is always filled with 1 when the symbol is shifted to the right; if it is 0, the leftmost is always filled with 0 when the symbol is shifted to the right.

The following table lists examples of shift operators.

 Operation Result Description 00110010 <2 11001000 Always fill 0 on the right 00110010> 2 00001100 Same results 00110010> 2 00001100 10110010> 2 11101100 Different Results 10110010> 2 00101100

The bitwise AND operator "&" performs logic and on two bits, and the bitwise OR operator "|" performs logic or on two bits, the bitwise XOR operator (^) is used to perform bitwise XOR operations on two bit strings. The calculation rules are shown in the following table.

 Bitwise AND By bit or Bitwise OR 0 & 0 = 0 0 | 0 = 0 0 ^ 0 = 0 0 & 1 = 0 0 | 1 = 1 0 ^ 1 = 1 1 & 0 = 0 1 | 0 = 1 1 ^ 0 = 1 1 & 1 = 1 1 | 1 = 1 1 ^ 1 = 0

Related Keywords:
Related Article

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.