java shift operation

     The object that the shift operator operates on is the binary bit, and the shift operator can be used alone to process the int type integer.

 operator       meaning       example      
<< Left shift operator, moves the object on the left side of the operator to the left by the number of bits specified on the right side of the operator (add 0 in the low order) x<<3
>> The "signed" right shift operator moves the object to the left of the operator to the right by the number of bits specified to the right of the operator. The sign extension mechanism is used, that is, if the value is positive, the high-order bits are filled with 0s, and if the value is negative, the high-order bits are filled with 1s. x>>3
>>> The "unsigned" right shift operator moves the object on the left side of the operator to the right by the number of bits specified on the right side of the operator. The 0 extension mechanism is adopted, that is, no matter the value is positive or negative, 0 is added to the high position. x>>>3

  In java, when byte, short, and long are shifted, they first convert their respective types to int, and then convert them to their corresponding types after they are converted to binary calculations.

  For example: 3<<3 = 0000 0011 * 2^3 = 0001 1000 = 24 , that is, move three places to the left, and add 0 to the low position

     24 >> 3 = 0001 1000 / 2^3 = 0000 0011 = 3, that is, move three bits to the right, and add 0 to the high bit  

    x<<y is equivalent to x*2 ; x>>y is equivalent to x/2 y

      In terms of computational speed, shift operations are faster than arithmetic operations.
      If x is negative, then x>>>3 has no arithmetic meaning, only logical meaning.

 

  In java, the data length of the int type is 32 bits. If the int type is shifted left or right by more than or equal to 32 bits, the data will not be filled with 1 or 0 as expected. The processing method of java is: when it is exactly an integer multiple of the data length, that is, 32, 64... Similarly, long type data is based on 64 changes. 
In practical applications, special attention should be paid to this point. Of course, this feature can also be used to implement the design of some special algorithms.

 operator       meaning       example      
<< Left shift operator, moves the object on the left side of the operator to the left by the number of bits specified on the right side of the operator (add 0 in the low order) x<<3
>> The "signed" right shift operator moves the object to the left of the operator to the right by the number of bits specified to the right of the operator. The sign extension mechanism is used, that is, if the value is positive, the high-order bits are filled with 0s, and if the value is negative, the high-order bits are filled with 1s. x>>3
>>> The "unsigned" right shift operator moves the object on the left side of the operator to the right by the number of bits specified on the right side of the operator. The 0 extension mechanism is adopted, that is, no matter the value is positive or negative, 0 is added to the high position. x>>>3

  In java, when byte, short, and long are shifted, they first convert their respective types to int, and then convert them to their corresponding types after they are converted to binary calculations.

  For example: 3<<3 = 0000 0011 * 2^3 = 0001 1000 = 24 , that is, move three places to the left, and add 0 to the low position

     24 >> 3 = 0001 1000 / 2^3 = 0000 0011 = 3, that is, move three bits to the right, and add 0 to the high bit  

    x<<y is equivalent to x*2 ; x>>y is equivalent to x/2 y

      In terms of computational speed, shift operations are faster than arithmetic operations.
      If x is negative, then x>>>3 has no arithmetic meaning, only logical meaning.

 

  In java, the data length of the int type is 32 bits. If the int type is shifted left or right by more than or equal to 32 bits, the data will not be filled with 1 or 0 as expected. The processing method of java is: when it is exactly an integer multiple of the data length, that is, 32, 64... Similarly, long type data is based on 64 changes. 
In practical applications, special attention should be paid to this point. Of course, this feature can also be used to implement the design of some special algorithms.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325337505&siteId=291194637