<< (move left), >> (move right) and >>> (unsigned Right shift)

Source: Internet
Author: User

The shift operator is the translation of numbers on the basis of binary.

Divide into three types according to the direction of translation and the rules for filling numbers:

<< (move left), >> (move with symbol right) and >>> (unsigned Right move).
In shift operations, the results of the byte, short, and char types shift to the int type, and when the byte, short, char, and int are shifted, the actual number of moves is the amount of movement and the remainder of the 32, which is the same result as the shift 33 times and the displacement 1 times. When you move a long value, you specify that the number of moves is the number of moves and the remainder of the 64, which is the same result of moving 66 times and moving 2 times.


The move rules for the three shift operators and their use are shown below:
<< Operation rules:

In binary form, all digits are moved to the left by the corresponding digits, the high position is removed (discarded), and the low vacancy is 0.
Syntax format:
Number of shifts << shifts required
For example: 3 << 2, then move the number 3 to the left 2 digits
Calculation process:
3<< 2
First, convert 3 to binary digits

0000 0000 0000 0000 0000 0000 0000 0011,

It then moves two 0 out of the high (left) of the number, and the other numbers move 2 digits to the left, and finally two vacancies in the low (right) 0. The final result is

0000 0000 0000 0000 0000 0000 0000 1100,

The conversion to decimal is 12.


>> Operation rules:

Move all numbers to the right in binary form, lower down (discard),

The vacancy complement sign bit of the high position, that is, positive number 0, negative complement 1.
Syntax format:
Number of shifts >> shifts required
For example one >> 2, move the number 11 to the right 2 bit
Calculation process:

The binary form of 11 is:

0000 0000 0000 0000 0000 0000 0000 1011,

Then move the last two digits out of the low, because the number is positive, so make up 0 in the high position. Then get the final

The result:

0000 0000 0000 0000 0000 0000 0000 0010.

Conversion to decimal is 2.


>>> Operation rules:

In binary form, all the digits are shifted to the right digits, the low position is removed (discarded), and the vacancy in the high position is 0.

Inserts 0 at the high level, whether positive or negative;

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.