Article From:https://www.cnblogs.com/coffees/p/9967804.html

Learning Java is a cumulative thing. Maybe you can master some fur technology and learn some basic aspects of Java through self-study, but to be proficient, you still need to accumulate your own technology and work experience.

  The technical knowledge shared today is: displacement operator in java!

Picture 1

  1) “The signed “left shift operator” (& lt; & lt;) can move the object on the left side of the operator to the number of digits specified on the right side of the operator (complement 0 at the low bit).

  Left shift is equivalent to multiplying by 2, for example

  3 << 2 //12 It shifts the number 3 to the left by 2 bits 3*2*2=3* (the second power of 2)

  Analysis: First convert 3 to binary digit 0000 0000 0000 0000 0000 0000 0000 0000 0011, then move the two zeros of the high digit (left side), the other digits move 2 digits to the left, and finally the two empties of the low digit (right side)Bit zero. The final result is 0000 0000 0000 0000 0000 0000 0000 1100, and the conversion to decimal system is 12. Mathematical meaning:

  On the premise that the number does not overflow, for both positive and negative numbers, the left shift is equal to the first power multiplied by 2, and the left shift is equal to the n power multiplied by 2.

  2) “The signed “right shift operator (>>) moves the object on the left of the operator to the number of digits specified on the right of the operator. The “signed” right shift operator uses the “symbol extension”: if the value is positive, 0 is inserted at the high bit; if the value is negative, 1 is inserted at the high bit.

  >>Operational rules: move all the digits to the right in binary form, low displacement (discarded), high vacancy complement bit, the digits obtained after the shift are positive and negative complement 0 and 1.

  For example, 11> & gt; 2 shifts the number 11 to the right by two bits.

  Analysis: The binary form of 11 is: 0000 0000 0000 0000 0000 0000 0000 1011, and then move the last two digits from the lower digits, because the digits are positive, so zero is added at the higher digits. The final result is 0000.Converting to decimal system is 3. Mathematical meaning: moving one bit to the right is equal to dividing 2, and moving n bit to the right is equal to dividing n power to 2.

  3) JavaA “unsigned” right shift operator (& gt; & gt; & gt;) is also added, which uses a “zero extension”: zero is inserted at high positions, whether positive or negative.

  4)A right shift is equal to dividing by 2, and a left shift is equal to multiplying by 2 (without spillover); the speed of shift operation is faster than that of multiplication and division operation.

  5)Priority of Bit Operators

  ~The highest priority is & lt; & lt; & gt; & gt; & gt; & gt; & gt; & gt; & amp; & then ^, the lowest priority is |.

Leave a Reply

Your email address will not be published. Required fields are marked *