Detailed description of shift operations in java

Source: Internet
Author: User

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]
  1. PublicclassShift {
  2. Publicstaticvoidmain (String [] args ){
  3. System. out. println ("****************** a positive number is shifted to the left to fill in 0 ************* ******");
  4. Inta = 1;
  5. A = a <2;
  6. System. out. println ();
  7. System. out. println (Integer. toBinaryString ());
  8. System. out. println ("****************** positive number shifted to a high value to 0 ************* *******");
  9. A = 1;
  10. A = a> 2;
  11. System. out. println ();
  12. System. out. println (Integer. toBinaryString ());
  13. System. out. println ("****** from the above results, we can see that shift is non-circular *****");
  14. System. out. println ("look at the shift of negative numbers :");
  15.  
  16. System. out. println ("*********** Add 1 **************");
  17. Inti =-1;
  18. System. out. println (I + ":");
  19. System. out. println (Integer. toBinaryString (I ));
  20. I = I> 2;
  21. System. out. println (I );
  22. System. out. println (Integer. toBinaryString (I ));
  23. System. out. println ("*********** negative left shift operation Low fill 0 *****************");
  24. I = I <2;
  25. System. out. println (I );
  26. System. out. println (Integer. toBinaryString (I ));
  27. System. out. println ("*************** and then look at> operator *************");
  28. System. out. println ("************* negative >>> add 0 *************** ");
  29. I =-1;
  30. System. out. println (Integer. toBinaryString (I ));
  31. I = I >>> 10;
  32. System. out. println (I + ":");
  33. System. out. println (Integer. toBinaryString (I ));
  34. System. out. println ("*************** Note: No <symbol **************");
  35.  
  36. System. out. println ("*********** forced conversion during byte type shift *************");
  37. Bytek = 10;
  38. System. out. println (Integer. toBinaryString (k ));
  39. K = (byte) k >>> 2 );
  40. System. out. println (Integer. toBinaryString (k ));
  41. }
  42. }
    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

     

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.