Bit operation learning notes:
Java median operators include,
<
>
>>>
&
|
~
^
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.
<Move left. Add 0.
> 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