Problems with the Java shift operator attention

Source: Internet
Author: User

If the values of the Char,byte or short type are shifted, they will be converted to the int type before the shift is made.

and the resulting result is also an int type.

If the value of long is processed, the result is long.

If this right-shift (>>>=) operation is performed on a byte or short value, the resulting result may be incorrect, and they will be converted to the INT type.

It is then truncated and assigned to the original type.

 Public classTest {/**     * @paramargs*/     Public Static voidMain (string[] args) {intI=-1;        System.out.println (integer.tobinarystring (i)); I>>>=10;        System.out.println (integer.tobinarystring (i)); byteB=-1;//11111111System.out.println (integer.tobinarystring (b));//Call an integer tobinarystring (int x) 32 1b>>>=10;//after the shift is 1111111111111111111111, because B is a byte type, so b= 11111111, or-1 valueSystem.out.println (integer.tobinarystring (b));//also output 32 x 1B=-1;        System.out.println (integer.tobinarystring (b)); System.out.println (integer.tobinarystring (b>>>10));//no assignment after shift, direct output, 22 x 1                        }    }    

11111111111111111111111111111111
1111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
1111111111111111111111

Problems with the Java shift operator attention

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.