Java << left shift,>> right shift,>>> unsigned right shift

Source: Internet
Author: User

>> Right Shift

Move right, the Tao in binary, assuming that a 32-bit int represents a 64, then the high is 0, so when we move the entire binary number right, such as 0100000 >> 2 = 0001000, you can see the number of two bits after the right shift becomes 8, you can analyze In fact, the right shift is an operation divided by 2.

Example: For non-2,4,8,16,64 the number can also be tested:

SYSTEM.OUT.PRINTLN (3 >> 1); SYSTEM.OUT.PRINTLN (5 >> 1); System.out.println (>> 1);

The results were 1,2,31.

So the right shift is a process that divides the 2, the right one is except once, N is the n-th.

Also note that the >> is signed , that is, its high-level complement is determined by the highest bit, positive number of the highest bit is 0, negative number of the highest bit is 1, so negative >> or negative.

Another example:

        System.out.println (5>>1);        System.out.println (63>>1);        System.out.println (63>>2);        System.out.println (63>>3);

Results:

231157

It can be seen that >>1 is divided by 2,>>2 by 2 divided by 2.

>>> Unsigned Right Shift

The principle is the same as above, and the difference is that its highs are always supplemented by the.

        SYSTEM.OUT.PRINTLN (3 >>> 1);        SYSTEM.OUT.PRINTLN (5 >>> 1);        System.out.println (>>> 1);        System.out.println (>>> 1);        System.out.println ( -63 >>> 1);

The results were:

1232312147483616

You can see that the >>> symbol is the same as the >> symbol when calculating a positive number, and moves the position change if it is negative

Since 64 exists in the computer as: 11111111111111111111111111000000, then the unsigned right shift one becomes 01111111111111111111111111100000, which is equal to 2147483616

So do not use inertial thinking to think about these bit operations, and be sure to understand how the binary works.

<< left Shift

System.out.println ( -64 << 1);  System.out.println (<< 1);  System.out.println (<< 1);

The result is -128,128,50, apparently by 2 .

The left post-shift low is added by the

3<<3 represents 3 times 2 of the three-square

Because moving a number to the left n-bit, it is equivalent to multiplying by 2 of the n-th square

There is no <<< in Java, since the low post left is definitely added by the

Java << left shift,>> right shift,>>> unsigned right shift

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.