Java Shift Operations

Source: Internet
Author: User

The object of the shift operator operation is the binary bit, which can be used to handle int integers with the shift operator alone.

Operator Meaning Example
<< Left-shift operator, moves the left-hand object to the left by the number of digits specified to the right of the operator (in the low-fill 0) X<<3
>> The signed right-shift operator moves the object to the left of the operator to the right by the number of digits specified to the right of the operator. Use the symbol extension mechanism, that is, if the value is positive, then the high is 0, and if the value is negative, the high is 1. X>>3
>>> The "unsigned" right-shift operator moves the object to the left of the operator to the right by the number of digits specified to the right of the operator. Use 0 expansion mechanism, that is, regardless of the value of the positive or negative, are in the high 0. X>>>3

In Java, Byte,short,long converts the respective type to int when the shift operation is converted to a binary calculation and converts it to the corresponding type.

such as: 3<<3 = 0000 0011 * 2^3 = 0001 1000 = 24, i.e. move left three bit, low 0

>> 3 = 0001 1000/2^3 = 0000 0011 = 3, that is, move to the right three bits, high fill 0

X<<y equivalent to x*2y, x>>y equivalent to x/2y

From the computational speed, the shift operation is faster than the arithmetic operation.
If x is negative, then x>>>3 has no arithmetic meaning, only logical meaning.

In Java, the data length of type int is 32 bits, and if you move the int type to the left or right by a value greater than or equal to 32 bits, the data is not populated as expected by 1 or 0. Java is handled in the following way: When the length of the data is exactly the number of times, that is, 32, 64, the data remains unchanged, in other cases, the length of the move divided by 32. In the same vein, the long type data has a 64 change benchmark.
In the practical application, it is necessary to pay special attention to this point, of course, you can use this feature to implement some special algorithm design.

Java Shift Operations

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.