Shift Operators in java

Source: Internet
Author: User

The shift operator performs translation in the binary form of a number. The options include left shift (<), right shift with symbols (>), and unsigned right shift (>>> ).
The operation rule of the Left shift operator <) is: shifts the number to the corresponding number of digits in binary form, discards the upper part, and zeros the lower part.
The operation rule for the right-shift operator (>) is: shifts the number right to the corresponding number of digits in binary form, discards the low position, and adds zero to the high positive number, and 1 to the negative number.
The operation rule of the unsigned right shift operator (>>>) is to shift the right digit of a number in binary form, discard the low part, and add the high part to zero.
[Java]
Int a =-8;
System. out. println (a <2 );
System. out. println (a> 2 );
System. out. println (a >>> 2 );
The output result is
-32
-2
1073741822
It is worth noting that the shift operation can be seen as the shift after the remainder of the number of digits of the type. In java, int Is 32-bit, and long is 64-bit. For example, the int type number is shifted to 40 BITs, which is the same as the 40% 32 = 8 bits. If the int type is shifted to 32 * n, nothing is done.
For byte, char, and short, the data type is automatically converted to int type during the shift operation. Therefore, for the following code, the output is 8 8 8 8 0
[Java]
Byte B = 8;
Short s = 8;
Char c = 8;
Int I = 8;
Long l = 8;
System. out. println (B> 32 );
System. out. println (s> 32 );
System. out. println (c> 32 );
System. out. println (I> 32 );
System. out. println (l> 32 );

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.