Java shift operator explanation [GO]

Source: Internet
Author: User
Tags bitwise

The Java shift operators are all about these three kinds of:<< (left shift), >> (with symbols right) and >>> (unsigned Right shift). 1 . Left shift operator left shift operator << the number of times that all bits of the specified value are shifted left. 1) Its general format is as follows: value << num num Specifies the number of bits to shift the value to move. Left-shift rule only remembers one point: Discard the highest bit, 0 the minimum bit if the number of bits moved exceeds the maximum number of digits of the type, the compiler will model the number of bits moved. If you move 33 bits to the int type, you actually move only 332 = 1 bits.

2) The arithmetic rules move all numbers to the left of the corresponding digits in binary form, the high position is shifted out (discarded), and the low vacancy is 0. When the left-hand operand is of type int, the 31st bit of each 1-bit shift is moved out and discarded, and when the left-hand operand is a long, the 63rd bit of each 1-bit move is removed and discarded. When the left-hand operand is byte and short, these types are automatically expanded to be of type int.

3) The mathematical significance of the number without overflow, for positive and negative, left one is equal to multiply by 2 of the 1, left to shift n is equal to 2 times the N-square

4) Calculation process: For example: 3 <<2 (3 for int) 1) Convert 3 to binary number 0000 0000 0000 0000 0000 0000 0000 0011, 2) Move the number high (left) two 0 out, the other numbers are shifted to the left 2 bits, 3) in the low Two seats (right) 0. The resulting result is 0000 0000 0000 0000 0000 0000 0000 1100, converted to decimal is 12.

The number of bits moved exceeds the maximum number of digits in the type, and if you move the higher-order bit (31 or 63 bits), the value becomes negative. The following procedure illustrates this point:

Left shifting as a quick-to-multiply by 2.    public class Multbytwo {public static void main (String args[]) {int i;    int num = 0xFFFFFFE;      for (i=0; i<4; i) {num = num << 1;    SYSTEM.OUT.PRINTLN (num); }   } }

The output of the program is as follows:

536870908 1073741816 2147483632-32 Note: n-bit binary, the highest bit is the sign bit, so the value range represented by -2^ (n-1)--2^ (n-1)-1, so the modulo is 2^ (n-1).

2 . Right-shift operator right-shift operator << the number of times that all bits of the specified value are shifted right. 1) Its general format is as follows: value >> num num Specifies the number of bits to shift the value to move. The rule that moves right only remembers one point: the symbol bit is unchanged, the left side is the symbol bit

2) Operational rules: According to the binary form of all the numbers to the right to move the corresponding number of digits, low-off (discard), high-level vacancy complement the sign bit, that is, positive 0, negative 1 when the operand to the right is the byte and short type, these types will be automatically expanded to int type. For example, if the value to be removed is negative, each right shift is 1 on the left, and if the value to be moved is a positive number, each right shift is 0 on the left, which is called the sign bit extension (sign extension), which moves right

The symbol used to hold negative numbers during operation.

3) The mathematical meaning of the right shift one equivalent to 2, the right shift n is equivalent to dividing by 2 of the n-th side.

4) The calculation process one >>2 (11 is int) 1) 11 of the binary form is: 0000 0000 0000 0000 0000 0000 0000 1011 2) The last two digits of the low position are shifted out, because the number is positive, so the high is 0. 3) The final result is 0000 0000 0000 0000 0000 0000 0000 0010. Conversion to decimal is 3.

>> 2 (35 int) 35 to binary: 0000 0000 0000 0000 0000 0000 0010 0011 Remove the last two digits of the low position: 0000 0000 0000 0000 0000 0000 0000 1 000 Convert to decimal: 8

5) When the right shift does not preserve the value of the symbol after the right shift to the 0x0f bitwise AND operation, so that any sign bit extension can be discarded, so that the resulting value can be used as the subscript to define the array, so that the corresponding array element represents the hexadecimal character. For example public class HexByte {public static public void main (String args[]) {char hex[] = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ' , ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f '}; byte B = (byte) 0xf1; System.out.println ("b = 0x" hex[(b >> 4) & 0x0f] Hex[b & 0x0f]);}}

(b >> 4) & 0x0f: The binary form of B is: 1111 0001 4 digits are removed: 0000 1111 Bitwise AND operation: 0000 1111 to 10 in form: 15

B & 0x0f: The binary form of B is: the binary form of the 1111 0001 0x0f is: 0000 1111 bitwise AND operation: 0000 0001 to 10 in form: 1

Therefore, the output of the program is as follows: B = 0xf1

3. Unsigned right-shift unsigned right-shift operator >>> its general format is as follows: value >>> num num Specifies the number of bits to shift the value to move. Unsigned right-shift rules only remember one point: ignoring the sign bit extension, the 0-bit unsigned right-shift operator >>> is only meaningful for 32-bit and 64-bit values

Source: http://blog.sina.com.cn/s/blog_4ad7c2540100ld9r.html

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.