Shift operators in Java and positive negative numbers conversions

Source: Internet
Author: User

Shift

In Java there are binary moves in the left (<<) and right Shift (>>)

Where right shift is also divided into signed right shift (>>) and unsigned Right shift (>>>)

1. Signed right shift: Move the binary to the right if the original sign bit is 0 then the new sign bit also uses 0 otherwise 1;

2. Unsigned right shift: Move the binary to the right regardless of the original sign bit is 0 or 1 will be used 0 complement;

Positive negative number conversion

As an example:

The binary of 3 is represented by a single byte

0000 0011

Normal a negative number of the method is: its positive number is reversed by a bitwise AND plus 1

0000 0011

Reverse: 1111 1100

Plus one: 1111 1101 (This is the binary representation of 3)


But there are quicker ways to convert:

Binary from left to right see the last 1 unchanged, all the rest take the reverse

0000 0011 (3)

1111 1101 (-3)

Post-Replenishment Knowledge:

When a byte type negative number is converted to an integer, the other 24 bits are filled with one, and the positive number is filled with zeros. If you want negative numbers to be filled with zero, you have to use this negative number with 0xFF and then convert to int type OK.



Shift operators in Java and positive negative numbers conversions

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.