Java Shift Operators

Source: Internet
Author: User

Important: 1. Convert the binary number before making the shift,

2. Negative number to the high position to use 1, positive number 0

Displacement number << number of shifts

First convert 3 to binary number 0000 0000 0000 0000 0000 0000 0000 0011, then move the number high (left) two 0, the other numbers are shifted to the left 2 bits, and finally on the low (right) two empty fill 0. The resulting result is 0000 0000 0000 0000 0000 0000 0000 1100, then the conversion to decimal is 12.

On the premise that the number is not overflow, for positive and negative numbers, the left one is the equivalent of multiplying by 2 by 1, and the left shift n is the equivalent of multiplying by 2 of the n-th square.

>>

According to the binary form of all the numbers to the right to move the corresponding displacement number, low-shift out (discard), high position of the empty fill sign, that is, positive 0, negative 1.
Syntax format:
Number of digits >> shifts that need to be shifted
For example, one >> 2, the number 11 is shifted to the right 2 bits
Calculation process:
The binary form of 11 is: 0000 0000 0000 0000 0000 0000 0000 1011, then the last two digits of the low position are shifted out, since the number is positive, so the high is 0. The resulting result is 0000 0000 0000 0000 0000 0000 0000 0010. Conversion to decimal is 2. L >>>
Operation rules:
In binary form, all numbers are shifted to the right by the corresponding number of digits, the low position is shifted out (discarded), and the upper slot is 0. For positive numbers, it is the same as a signed right shift, which is different for negative numbers.

Java Shift Operators

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.