http://blog.csdn.net/xiaochunyong/article/details/7748713
The bitwise operators provided by Java are: Left (<<), right Shift (>>), unsigned Right shift (>>>), Bit and (&), bit or (|), bit non (~), bitwise XOR (^), except for bits (~), which are unary operators. All the others are two-dollar operators.
1. Shift Left (<<)
Test1, move 5 to the left 2 bits:
[Java]View PlainCopy
- Package com.xcy;
- Public class Test {
- public static void Main (string[] args) {
- System.out.println (5<<2); The result of the operation is
- }
- }
The result is 20, but how does the program work?
First, the 5 is converted to a 2-binary representation (in Java, integers are by default the int type, or 32-bit):
0000 0000 0000 0000 0000 0000 0000 0101 Then shift left 2 bit, low 0:
0000 0000 0000 0000 0000 0000 0001 0100 Converted to 10 binary 20
2, right Shift (>>), right shift the same, but the direction is not the same (feel and not say the same)
[Java]View PlainCopy
- System.out.println (5>>2); Operation result is 1
or first turn 5 into a 2 binary representation:
0000 0000 0000 0000 0000 0000 0000 0101 Then right-shift 2-bit, high-fill 0:
0000 0000 0000 0000 0000 0000 0000 0001
3, unsigned Right shift (>>>)
We know that in Java, the int type occupies 32 bits, can represent a positive number, or it can represent a negative number. A positive number is converted to binary with a maximum of 0, and the binary of negative numbers is up to 1
For example, 5 is converted into binary after:
1111 1111 1111 1111 1111 1111 1111 1011 (at the beginning of contact with the binary, I do not know that the highest bit is used to denote the positive and negative points, it was not always possible to understand. What you get is a positive number-_-).
We move the 5 right 3 bits, 5 to the right 3 bits and the unsigned right shift 3 bits respectively:
[Java]View PlainCopy
- Package com.xcy;
- Public class Test {
- public static void Main (string[] args) {
- System.out.println (5>>3); The result is 0 .
- System.out.println (-5>>3); The result is -1 .
- System.out.println (-5>>>3); The result is 536870911 .
- }
- }
Let's take a look at the shift process (which can be compared by converting the results into binary):
5 conversion into binary: 0000 0000 0000 0000 0000 0000 0000 0101
5 The binary with the result of 0,0 3-bit right shift is: 0000 0000 0000 0000 0000 0000 0000 0000//(complement with 0)
-5 conversion into binary: 1111 1111 1111 1111 1111 1111 1111 1011
-5 Right-shift 3-bit results in binary for -1,-1:1111 1111 1111 1111 1111 1111 1111 1111//(with 1 for complement)
-5 Unsigned right-shifted 3-bit result 536870911 converted to binary: 0001 1111 1111 1111 1111 1111 1111 1111//(with 0 for complement)
After the result is converted into binary, we can find that positive shift, high with 0 complement, negative right shift, high with 1 complement, when negative use unsigned right shift, with 0 of the position (naturally, the negative number becomes a positive number)
Note: The author is here to say the right shift, high-level complement situation. Positive or negative shifts left, and low is 0. (Self-test)
4, Bit and (&)
[Java]View PlainCopy
- Package com.xcy;
- Public class Test {
- public static void Main (string[] args) {
- System.out.println (5 & 3); Result is 1
- }
- }
or old routine, convert 2 operands and results to binary for comparison:
5 conversion to binary: 0000 0000 0000 0000 0000 0000 0000 0101
3 conversion to binary: 0000 0000 0000 0000 0000 0000 0000 0011
-------------------------------------------------------------------------------------
1 conversion to binary: 0000 0000 0000 0000 0000 0000 0000 0001
Bits and: The nth bit of the first operand in the second operand if all is 1, then the result of Nth is also 1, otherwise 0
5, bit or (|)
[Java]View PlainCopy
- Package com.xcy;
- Public class Test {
- public static void Main (string[] args) {
- System.out.println (5 | 3); //result is 7
- }
- }
5 conversion to binary: 0000 0000 0000 0000 0000 0000 0000 0101
3 conversion to binary: 0000 0000 0000 0000 0000 0000 0000 0011
-------------------------------------------------------------------------------------
7 conversion to binary: 0000 0000 0000 0000 0000 0000 0000 0111
Bit or operation: the nth bit of the first operand is at the nth digit of the second operand as long as there is a 1, then the result of Nth is also 1, otherwise 0
6, Bit XOR or (^)
[Java]View PlainCopy
- Package com.xcy;
- Public class Test {
- public static void Main (string[] args) {
- System.out.println (5 ^ 3); Result is 6
- }
- }
5 conversion to binary: 0000 0000 0000 0000 0000 0000 0000 0101
3 conversion to binary: 0000 0000 0000 0000 0000 0000 0000 0011
-------------------------------------------------------------------------------------
6 conversion to binary: 0000 0000 0000 0000 0000 0000 0000 0110
Bitwise XOR: The nth position of the first operand is the opposite of the nth bit of the second operand, then the nth of the result is also 1, otherwise 0
7, bit non (~) bit is not a unary operator
[Java]View PlainCopy
- Package com.xcy;
- Public class Test {
- public static void Main (string[] args) {
- SYSTEM.OUT.PRINTLN (~5); Result is-6
- }
- }
5 conversion to binary: 0000 0000 0000 0000 0000 0000 0000 0101
-------------------------------------------------------------------------------------
6 conversion to binary: 1111 1111 1111 1111 1111 1111 1111 1010
Bit non: the nth bit of the operand is 1, then the nth bit of the result is 0, and vice versa.
Derived from bitwise operator operators are:
&= Bitwise AND Assignment
|= Bitwise OR Assignment
^= bitwise non-assigned value
>>= Right Shift Assignment
>>>= Unsigned Right Shift assignment
<<= Assignment Shift Left
and + = a concept only.
As an example:
[Java]View PlainCopy
- PACKAGE&NBSP;COM.XCY;&NBSP;&NBSP;
-
- public class test {
- public static void main (string[] args) {
- int a = 5&NBSP;&NBSP;
- a &= 3;&NBSP;&NBSP;
- system.out.println (a); //result is 1
- }
- }&NBSP;
[Turn]java bit Operations (1)