Note:
> 1 and <1
The shift operator is to translate a number based on binary. There are three types of rules based on the direction of translation and the number filling rule: <(left shift),> (shifted right with a symbol) and> (shifted right without a symbol ).
During the shift operation, the result after the byte, short, and char types are shifted to the int type. When the byte, short, Char, and INT types are shifted, the actual number of moves is the number of moves and the remainder of 32, that is, the result of 33 shifts is the same as that of 1 shift. When moving a long value, the actual number of moves is the number of moves and the remainder of 64, that is, the result of moving 66 times is the same as that of moving 2 times.
The following table describes the movement rules and usage of the three shift operators:
<Operation rule: in binary format, all digits are moved to the left to the corresponding number of digits. The high position is removed (discarded) and the low position is filled with zero.
Syntax format:
Number to be shifted <number of shifts
For example, if the value is 3 <2, the value 3 is shifted to two places.
Computing process:
3 <2
First, convert 3 to a binary number 0000 0000 0000 0000 0000 0000 0000 0011. Then, remove the two zeros in the upper (left) Position of the number. All the other digits are shifted to the left, at last, the two vacancies in the low position (on the right) are filled with zeros. The final result is 0000 0000 0000 0000 0000 0000 0000 1100, And the decimal value is 12. mathematical significance:
If the number does not overflow, for positive and negative numbers, moving one to the left is equivalent to multiplying the power of 1 by 2, and moving n to the left is equivalent to multiplying the power of N2.
> Operation rule: in binary format, all numbers are moved to the right to correspond to the steep digits. The low position is removed (discarded). The high position is supplemented with the sign bit, that is, positive number is supplemented with zero, and negative number is supplemented with 1.
Syntax format:
Number to be shifted> Number of shifts
For example, if the value is 11> 2, the number 11 is shifted to two places.
Calculation process: the binary format of 11 is 0000 0000 0000 0000 0000 0000 0000 1011. Then, the last two digits of the low position are removed. Because the number is a positive number, it is set to zero at the high position. The final result is 0000 0000 0000 0000 0000 0000 0000 0010. Converting to decimal is 3. mathematical meaning: Shifts one digit to the right is equivalent to dividing 2, and shifts n digits to the right is equivalent to dividing by the Npower of 2.
>>> Operation rule: in binary format, all numbers are moved to the right to correspond to the steep digits. The low position is removed (discarded), and the high position is filled with zero. For a positive value, it is the same as the shifted right value. For a negative number, it is different.
Other structures are similar to>.
Summary
Binary operators, including bitwise operators and shift operators, enable programmers to operate numbers on the basis of binary, perform operations more effectively, and store and convert data in binary format, is the basis for implementing network protocol resolution and encryption algorithms.
For example:
> It is a bitwise operator. you convert 8 to binary, and then shift one digit to the right.
0000 1000 8
0000 0100 4
Android applications:
Write text at a rect center:
Private paint Pt = new paint ();
Private rectf rect = new rectf ();
Int itextposx = (INT) rect. Right-(INT) pt. measuretext (sdate );
Int itextposy = (INT) rect. Bottom + (INT) (-Pt. ascent ())
-Gettextheight ();
Itextposx-= (INT) rect. Width ()> 1)
-(INT) pt. measuretext (sdate)> 1 );
Itextposy-= (INT) rect. Height ()> 1)-(gettextheight ()> 1 );
Canvas. drawtext (sdate, itextposx, itextposy + imargin, pt );