Demo and summary of Shift Operators in java, and operator demo

Source: Internet
Author: User

Demo and summary of Shift Operators in java, and operator demo

First, there are three types of shift operators: byte, short, char, int, and long.

<Left shift operator, which indicates to move the binary data of the left operand to the left * bit. After moving, the vacant bit is filled with 0, and the excess bit is discarded. (Equivalent to the Npower of 2)

> Right shift operator. How many digits does the binary data erase after the binary data is shifted * to the right? (This is a good decision, but I personally understand it) (equivalent to the power of Npower except 2)

>>> The unsigned right shift operator fills the left-side vacant space with 0, regardless of whether the highest bit before the move is 0 or 1.

Here is a demo to help you understand:

Public static void main (String [] args) {int a = 16; int B = a <2; // shift left 2, equivalent to the 2nd power of 16*2, that is, 16*4 int c = a> 2; // shifts 2 to the right, equivalent to the 2nd power of 16/2, that is, 16/4 System. out. println ("the binary data of a is:" + Integer. toBinaryString (a); System. out. println ("binary data after a shifts left:" + Integer. toBinaryString (B); System. out. println ("a right-shifted binary data:" + Integer. toBinaryString (c); System. out. println ("the value after a shifts left is:" + B); System. out. println ("the value after right shift of a is:" + c );}}

View the running result ,:

First, the binary data of 16 is 10000;

Shift two places left, 10000 to 1000000;

Shifts two places to the right, and 10000 to 100;

After the binary data is converted to normal data,

The value after a shifts left is 64, which is 16x2 to the power of 2 (16x4 ).

The value after the right shift of a is 4, which is the 2nd power of 16/2 (16/4 ).

After reading this demo, I can understand whether the shift operator is much simpler?

Related Article

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.