A summary of the shift operators in Java _java

Source: Internet
Author: User

There are three kinds of shift operators in Java

<<: Left-shift operator, NUM << 1, equivalent to num multiplied by 2

>>: Right shift operator, NUM >> 1, equivalent to num divided by 2

>>>: Unsigned right shift, ignoring symbol bit, vacancy is 0

Let's take a look at how these shift operations are used

Copy Code code as follows:

/**
*
*/
Package com.b510.test;

/**
* @author Jone Hongten
* @create Date:2013-11-2
* @version 1.0
*/
public class Test {

    public static void Main (string[] args) {
        int Number = 10;
       //RAW number binary
        printinfo ( number);
        number = number << 1;
       //Move left one
        printinfo ( number);
        number = number >> 1;
       //Right-move one
        printinfo ( number);
   }

   /**
     * Output binary number of an int
     * @param num
     */
    private static void printinfo (int num) {
         System.out.println (integer.tobinarystring (num));
   }
}

The results of the operation are:

1010
10100
1010 We align the above results:

43,210 digits--------
10,100 in: 10 original number
101,000 binary: 20 left one number = number << 1;
10,100 in: 10 to the right one number = number >> 1;

Look at the demo above, now is not on the left and right move to understand a lot of it

For:>>>

Unsigned right shift, ignoring sign bit, empty space with 0

Value >>> num--num Specifies the number of digits to shift the value of.

The rule of unsigned right shift only remembers one point: the symbol bit extension is ignored, the 0 complement highest bit unsigned right shift operator >>> is only meaningful for 32-bit and 64-bit values

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.