Demo and summary of Shift operators in Java (recommended) _java

Source: Internet
Author: User

First, there are three kinds of shift operators whose operation type supports only: Byte/short/char/int and long five.

<< left operator, which means that the binary data of the left-hand operand is moved to the left * bit, the empty vacancy is filled with 0, and the redundant bits are discarded . (Equal to the N-second party multiplied by 2)

>> Right shift operator, binary data to the right to move the * bit, after the second data to wipe out a few? (This is a good set, but it's a personal understanding)(equivalent to the N-second side of 2)

>>> the unsigned right-shift operator, regardless of whether the top bit is 0 or 1 before the move, the empty portion produced on the left side of the right shift is filled with 0来.

Here we'll take a demo to help understand:

public static void Main (string[] args) {
    int a =;
    int B = A << 2;//left 2, equivalent to 2 times of 16 * 2, that is, 4
    int c = A >> 2;//right Shift 2, which is equivalent to 16/2 of 2 times, that is, 16/4
    
    SYSTEM.OUT.P Rintln ("A's binary data is:" + integer.tobinarystring (a));
    System.out.println ("a left-shifted binary data is:" + integer.tobinarystring (b));
    System.out.println ("A right-shifted binary data is:" + integer.tobinarystring (c));
    
    System.out.println ("A left-shift value is:" + b);
    System.out.println ("A right-shifted value is:" + C);
  }

View the results of the run, as shown in the figure:

First, the binary data of 16 is: 10000;

Two left, 10000 into 10000;

Move right two digits, 10000 to 100;

After converting the resulting binary data into normal data,

A left-shift value is 64, 16 * 2 of the 2-second party (16 * 4).

The value of a right shift is 4, which is 16/2 of the 2-second square (16/4).

Is it much simpler to read the demo and understand the shift operator?

Above this Java on the shift operator demo and summary (recommended) is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.