Detailed description of shift operations in java
The shift operation in java is only valid for int and long. The values of byte, short, and char are upgraded to int before the shift.
There are three shift operators: >>( right shift), <(left shift), >>> (right shift). Note two points: 1. No <symbol 2. Shift is non-circular.
The three differences are:
>>> It refers to shift to the right with a symbol, fill 1 with a negative high, and fill 0 with a positive number ----- >>> that is, fill with a symbol bit in a high position.
<Left shift, no matter the negative or positive number, always add 0 at the low position
>>> It is a shift without symbols to the right. Whether it is a negative number or a positive number, a value of 0 is added for a high position (shifts right without symbols. If the sign bit is ignored, the blank space is filled with 0)
In a specific operation, all operations are converted to the complement logic for shift or bitwise operations.
Test procedure:
[Java]
- PublicclassShift {
- Publicstaticvoidmain (String [] args ){
- System. out. println ("****************** a positive number is shifted to the left to fill in 0 ************* ******");
- Inta = 1;
- A = a <2;
- System. out. println ();
- System. out. println (Integer. toBinaryString ());
- System. out. println ("****************** positive number shifted to a high value to 0 ************* *******");
- A = 1;
- A = a> 2;
- System. out. println ();
- System. out. println (Integer. toBinaryString ());
- System. out. println ("****** from the above results, we can see that shift is non-circular *****");
- System. out. println ("look at the shift of negative numbers :");
-
- System. out. println ("*********** Add 1 **************");
- Inti =-1;
- System. out. println (I + ":");
- System. out. println (Integer. toBinaryString (I ));
- I = I> 2;
- System. out. println (I );
- System. out. println (Integer. toBinaryString (I ));
- System. out. println ("*********** negative left shift operation Low fill 0 *****************");
- I = I <2;
- System. out. println (I );
- System. out. println (Integer. toBinaryString (I ));
- System. out. println ("*************** and then look at> operator *************");
- System. out. println ("************* negative >>> add 0 *************** ");
- I =-1;
- System. out. println (Integer. toBinaryString (I ));
- I = I >>> 10;
- System. out. println (I + ":");
- System. out. println (Integer. toBinaryString (I ));
- System. out. println ("*************** Note: No <symbol **************");
-
- System. out. println ("*********** forced conversion during byte type shift *************");
- Bytek = 10;
- System. out. println (Integer. toBinaryString (k ));
- K = (byte) k >>> 2 );
- System. out. println (Integer. toBinaryString (k ));
- }
- }
Running result:
***************** Positive number shifted to the left position to add 0 **************** ***
4
100
* **************** Add 0 to the right shift of a positive number in a high position **************** ****
0
0
* ***** From the above results, we can see that shift is non-circular *****
See the shift of negative numbers:
* *********** The right-shift operation of a negative number fills in 1 **************
-1:
11111111111111111111111111111111
-1
11111111111111111111111111111111
*****************
-4
11111111111111111111111111111100
* ************ Let's take a look at> operator *************
* ************ Negative >>> add 0 ***************
11111111111111111111111111111111
4194303:
1111111111111111111111
* *********** Note: No <symbol **************
* ********* Strong conversion during byte type shift *************
1010
10