Java SHIFT and XOR operations

Source: Internet
Author: User
Types of Java Shift operations

Basics: We know that in Java, the int type occupies 32 bits, can represent a positive number, or it can represent a negative number. A positive number is converted to binary with a maximum of 0, and the binary of negative numbers is up to 1
Example:
-5 converted to binary after: 1111 1111 1111 1111 1111 1111 1111 1011
How is the binary of negative numbers converted?
Negative numbers are converted to binary, that is, each bit of the complement of its opposite number (positive) is reversed (1 to 0,0 to 1) and the final value is added 1, which completes the complement operation of the negative. This becomes the binary.
5 converted to binary after: 0000 0000 0000 0000 0000 0000 0000 0000 0101

  1. Shift Right >>
    Positive shift right, high 0
    Example: 5>>3-->0000 0000 0000 0000 0000 0000 0000 0000 0000-->0
    Negative number right shift, high fill 1
    Example: -5>>3-->1111 1111 1111 1111 1111 1111 1111 1111-->-1
  2. Unsigned Right Shift >>>
    Unsigned right shift, high fill 0
    Example: -5>>>3-->0001 1111 1111 1111 1111 1111 1111 1111-->536870911
  3. Positive negative shift left <<
    Positive negative shift left is low 0
  4. Bit XOR or
    Bitwise XOR: The nth position of the first operand is the opposite of the nth bit of the second operand, then the nth of the result is also 1, otherwise 0

Java SHIFT and XOR 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.