Java Shift Operator Detailed example--left shift operator >>, signed right shift operator >>__java

Source: Internet
Author: User

The shift operator is also for the binary "bit", which mainly includes the left shift operator (<<), the right shift operator (>>>), and the signed right shift operator (>>).


1. Left shift operator
The left-shift operator, represented by "<<", is the object to the left of the operator, the number of digits to the right of the left operator, and the low 0. In fact, moving the n bit to the left is equivalent to multiplying by 2 of n times, such as the following example.
public class Data17
{
public static void Main (string[] args)
{
int a=2;
int b=2;
The result of System.out.println ("A shift is:" + (a<<b));
}
}
Run results
The result of a shift is: 8
Analyze the above program section:
First of all, in essence, 2 of the binary is 00000010, it moves to the left 2 bits, it becomes 00001000, that is 8. If you look at it from another angle, it moves 2 digits to the left, which is actually multiplying by 2 of the 2 times, and the result is 8.


2. Right shift operator
The right shift operator, represented by the symbol ">>>", is the specified number of digits to the right of the object to the left of the operator, and at the top of 0, in fact, the right n bit, which is equal to the n-th side of the 2.
public class Data18
{
public static void Main (string[] args)
{
int a=16;
int b=2;
The result of System.out.println ("A shift is:" + (a>>>b));
}
}
Run results
The result of a shift is: 4
Analysis of the above program section: In essence, 16 binary is 00010000, it moved to the right 2 bits, it becomes 00000100, that is 4. If you look at it from another angle, it moves 2 bits to the right, which is actually divided by 2 of the 2 times, and the result is 4.


3. Signed right-shift operator
The signed right shift operator is represented by the symbol ">>", which is the operand to the left of the operator, and the specified number of digits to the right of the operator. If a positive number, in the high position of 0, if the negative, then the high level of 1, first look at a simple example below.
public class Data19
{
public static void Main (string[] args)
{
int a=16;
int c=-16;
int b=2;
int d=2;
System.out.println ("A shift Result:" + (a>>b));
System.out.println ("C's shift Result:" + (c>>d));
}
}
Run results
Shift result of a: 4
Shift result of C:-4
Analyze the above program section:
The value of a is 16, converted to binary is 00010000, let it move to the right two digits 00000100 is 4.
The value of C is-16, converted to binary is 11101111, let it move to the right one into 11111011-4.

 

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.