<<

  • <<: left shift operation, left shift a few bits to fill a few zeros

>>

  • > > :Right shift operation, arithmetic right shift
    • If the number is positive, 0 is added in front of the shift
    • If the number is negative, it is replaced by 1

>>>

  • > > > : An unsigned right shift is a logical right shift. Ignore the sign, void fill 0
  • Unsigned right shift rule: The sign bit extension is ignored,0 complements the highest bit, and the unsigned right shift operator >>> is meaningful only for 32-bit and 64-bit values
  • If the number being shifted is positive:
    • Right shift and unsigned right shift have the same value
  • If the number being shifted is negative:
    • It’s still going to be negative
    • Unsigned right shift values are positive
  • The difference between:
    • There is no difference between >> and >>> for positive numbers
    • For negative numbers, the sign bit of the highest digit is ignored due to unsigned right shift. So:
      • -2 >>> 1 = 2147483647(Integer.MAX_VALUE)
      • -1 >>> 1 = 2147483647(Integer.MAX_VALUE)
    • Therefore, to determine whether two Integer numbers have the same symbol, you can do this:
    return ((a >> 31) ^ (b >> 31)) = =0;
    Copy the code

Result of shift operation

  • When a number is shifted n bits to the left not greater than the largest number of its numeric type, it is multiplied by 2 to the NTH power
  • A number shifts to the rightnBits, you just divide this number by2thenTo the power, and then to the whole
    • If the shift number exceeds the maximum number of digits of its own numeric type, simply apply the method to the number obtained by modulating the shift number and the maximum number of digits of its own numeric type

Pay attention to

  • The three shift operations operate on five left-hand operands:
    • long
    • int
    • short
    • byte
    • char
    • The specific operation process varies according to the operand type. The following principles should be followed:
      • intWhen it shifts, the left-hand operand is32Bit, where the shift symbol operates on32positionbiton
        • For example, if 1 >> 3, move the 32 bits 00000000 00000000 00000000 00000001 to the right by 3 bits
      • When long shifts, the left-hand operand is 64 bits, and the shift symbol acts on 64 bits
      • short,byte,charThe data is first converted to before the shiftint,And then it shifts, where the shift sign operates on theta32positionbiton
        • For example, if (byte) 0xFF >>> 7, the 32 bits 111111111111 11111111 11111111 are moved 7 bits to the right. The result is 00000001 1111111111 11111111 11111111
  • As can be seen from the above:
    • When the left-hand operand is long, the resulting type is long
    • When the left-hand operand is of the other four types, the resulting type is shiftedint
      • So when the left-hand operand is short,byte, or char, use >>=, >>>=, <<=. In fact, it is the value of the int to do low interception, the value often wrong
  • The three shift symbols have rules for the right operand as well as the left operand:
    • If the left-hand operand isintOr after the conversionint,So the right-hand operand is only low5Bits are valid, because int is total32position
      • 22 >> 33 is the same as 22 >> 1, both are 11
    • If the left-hand operand is long, then only the lower 6 bits of the right-hand operand are valid