Java programming 26-Shift Operators

Source: Internet
Author: User

Java programming those things 26-Shift Operators Zhengzhou game college Chen yuefeng from: http://blog.csdn.net/mailbomb 4.5.2 Shift OperatorThe shift operator is to translate a number based on binary. There are three types of rules based on the direction of translation and the number filling rule: <(left shift),> (shifted right with a symbol) and> (shifted right without a symbol ). During the shift operation, the result after the byte, short, and char types are shifted to the int type. When the byte, short, Char, and INT types are shifted, the actual number of moves is the number of moves and the remainder of 32, that is, the result of 33 shifts is the same as that of 1 shift. When moving a long value, the actual number of moves is the number of moves and the remainder of 64, that is, the result of moving 66 times is the same as that of moving 2 times. The following table describes the movement rules and usage of the three shift operators. <operation rules: shifts all digits to the right in binary form, and removes the digits from the upper position (discard ), fill zero for low positions. Syntax format: number to be shifted <number of shifts for example: 3 <2, the calculation process is to shift the number 3 to the left: 3 <2 first convert 3 to a binary number 0000 0000 0000 0000 0000 0000 0000 0011, and then remove the two zeros of the numeric high (left, all other numbers are shifted to the left, and the two vacancies at the low position (on the right) are filled with zeros. The final result is 0000 0000 0000 0000 0000 0000 0000 1100, which is converted to 12 in decimal format. Mathematical significance: without a number overflow, for positive and negative numbers, shifts one digit left is equivalent to multiplying the power of 1 of 2, and shifts n digits left is equivalent to multiplying the power of N2. L> operation rule: in binary format, all numbers are moved to the right to the corresponding number of digits, and the low position is removed (discarded). The high position is filled with the sign bit, that is, the positive number is supplemented with zero, and the negative number is supplemented with 1. Syntax format: number to be shifted> Number of shifts, for example, 11> 2, the calculation process is to shift the number 11 to the right. The binary format of the calculation process is: 0000 0000 0000 0000 0000 0000 0000, and then remove the last two digits of the low position. Because the number is positive, it is set to zero at the high position. The final result is 0000 0000 0000 0000 0000 0000 0000 0010. Convert to 3 in decimal format. Mathematical meaning: Shifts one digit to the right is equivalent to dividing 2, and shifts n digits to the right is equivalent to dividing by 2 to the Npower. L >>> operation rule: in binary format, all numbers are moved to the right to correspond to the steep digits, the low positions are removed (discarded), and the high positions are filled with zero. For a positive value, it is the same as the shifted right value. For a negative number, it is different. Other structures are similar to>. 4.5.3 SummaryBinary operators, including bitwise operators and shift operators, enable programmers to operate numbers on the basis of binary, perform operations more effectively, and store and convert data in binary format, is the basis for implementing network protocol resolution and encryption algorithms. However, in practice, to make the code readable, we still use a large number of ordinary arithmetic operators for numerical 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.