Shift operations of char, byte, and short in Java

Source: Internet
Author: User

Java in think has the following sentence:

If char, byte, or short is displaced, they are automatically converted to an int before the shift. Only the five low positions on the right can be used. This prevents us from moving the unrealistic digits in an int number.

 

In the above sentence, the object operated by the shift operator is the binary "bit", and the int type is 32 bits, that is, the 32 power of 2. If you move more than 32 bits, all the original data will disappear, which makes no sense.
So the above "only the five low positions on the right will be useful" means that the lower five digits of the shift operator's right side (converted to binary) will be useful, that is, x <y indicates that y can be used only when its 5th bit is low, that is, it cannot be greater than 32. The same applies to long.

Therefore, if you shift an int type, x <Y;
When Y is less than 32, the result after the shift is generally in the range that can be expressed. Then, convert Y to the binary number, and then take the lower five digits of the binary number, then convert the five digits into decimal digits, which is the number of digits to be moved to X.

For example, int A = 140;
System. Out. println (integer. tobinarystring (A <34 ));
The execution process of the preceding statement is:
First, convert a into a binary number: 1000 1100
When executing Statement A <34, convert 34 to binary 10 0010 first, and convert the binary number to a decimal number 2 with a lower value of 5 digits of 0 0010, therefore, it is actually two shifts to the left of.

Now everyone on Earth knows that the result is: 10 0011 0000

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.