Java Shift Operators

Source: Internet
Author: User

From: http://darrendu.iteye.com/blog/773072

"<", ">>", And" >>> "in Java are operators for moving left, signed right, and unsigned right. The offset operator only operates on the int value. If it is not an int value, the compiler reports an error. In Java, the length of an int is always 32 bits, that is, 4 bytes.

(1) left moving OPERATOR:
The number of operations is moved to the left. The number of BITs to be moved is specified by the left operand,The low position is automatically set to zero (0 ).
(2) right moving OPERATOR:
In turn, the operand is moved to the right, and the number of bits to be moved is also specified by the right operand.Note: In the face of numbers with positive and negative numbers, the symbol extension is used. If the original value is positive, 0 is added to the high value. If the original value is negative, 1 is added to the high value.
(3) right shift operator without plus or minus signs (>>> ):
Supplement with 0, meaning,Both positive and negative signs are filled with 0 at a high level.

Below I want to parse these three operators in detail:

1. Preparation knowledge

Able to skillfully convert Positive and Negative binary codes;

Conversion principle:Anti-increment "1"

If you have any questions, seeArticleContent in "Java Basic Knowledge series 3"

2: Use instances to describe analysis operations.

Public classTest {

Public static voidMain (string [] ARGs ){

IntM =-7;

System.Out. Println ("M's binary code is:" + integer.Tobinarystring(M ));

System.Out. The binary code of println ("m> 2 is:" + integer.Tobinarystring(M> 2 ));

System.Out. Println ("(M> 2) =" + (M> 2 ));

System.Out. Println ("m <2 binary code is:" + integer.Tobinarystring(M <2 ));

System.Out. Println ("(m <2) =:" + (M <2 ));

System.Out. Println ("m >>> the binary code of 24 is:" + integer.Tobinarystring(M> 24 ));

System.Out. Println ("m >>> 24:" + (M >>> 24 ));

}

}

Execution result:

M binary code: 11111111111111111111111111111001

The binary code of M> 2 is: 11111111111111111111111111111110

(M> 2) =-2

M <2 binary code is: 11111111111111111111111111100100

(M <2) =:-28

M >>> the binary code of 24 is: 11111111

M >>> 24: 255

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.