Understanding Java Shift Operators

Source: Internet
Author: User
Tags arithmetic bitwise bitwise operators

The object of the shift operator operation is the binary bit, which can be used to handle int integers with the shift operator alone.

Operator Meaning Example
<< Left-shift operator, moves the left-hand object to the left by the number of digits specified to the right of the operator (in the low-fill 0) X<<3
>> The signed right-shift operator moves the object to the left of the operator to the right by the number of digits specified to the right of the operator. Use the symbol extension mechanism, that is, if the value is positive, then the high is 0, and if the value is negative, the high is 1. X>>3
>>> The "unsigned" right-shift operator moves the object to the left of the operator to the right by the number of digits specified to the right of the operator. Use 0 expansion mechanism, that is, regardless of the value of the positive or negative, are in the high 0. X>>>3

  

Take 6297 of the int type as an example, with the following code:

[Java] view Plaincopy
    1. system.out.println (integer.tobinarystring (6297));    
    2. system.out.println (integer.tobinarystring ( -6297));    
    3. system.out.println (integer.tobinarystring (6297> >5));    
    4. system.out.println ( Integer.tobinarystring ( -6297>>5));    
    5. system.out.println (integer.tobinarystring ( -6297>>>5));    
    6. system.out.println (integer.tobinarystring (6297<<5));   
    7. system.out.println (integer.tobinarystring ( -6297<<5));   

Operation Result:

1100010011001
11111111111111111110011101100111
11000100
11111111111111111111111100111011
11000100
111111111111111111100111011
110001001100100000
11111111111111001110110011100000

Note: x<<y equivalent to x*2y, x>>y equivalent to x/2y
From the computational speed, the shift operation is faster than the arithmetic operation.
If x is negative, then x>>>3 has no arithmetic meaning, only logical meaning.

In the think in Java there is a passage

"Char,byte or short is shifted so that they are automatically converted to an int before the shift is made." Only the 5 lows on the right side will be useful. This prevents us from moving the impractical number of digits in an int number. If a long value is processed, the last result is a long. Only the 6 lows on the right are used to prevent moving beyond the existing number of digits in the long value. ”

This passage has two sources, one is the Java programming Idea 3.11 shift operator appears, the exact word is "only the value of the right side of the low 5-bit is useful." One is the Java Puzzle 27: The unpredictable I value, the exact words are "shift operators use only the lower 5 bits of the right operand as the shift length."

To figure this out first, we need to figure out the shift operator, the shift operator is a two-dollar operator, and the two operands are located on both sides of the shift operation, such as the right operand of the left operand shift operator, which means that the left operand is shifted the number of times specified by the right operand according to the shift direction specified by the shift operator. Then the reference to the source two, the Java doubts described in the enlightened.

First, the number of operations that the shift operator can manipulate is only int and long, which is the type of the left-hand operand. for int type, int is 4 bytes in Java, altogether 32 bits, that is, for an int data in Java, do 32 shifts, then this int data is completely changed, with left shift as an example, left is the complement 0, then for any int type data, do 32 shifts, Then the int data becomes 32 bits full 0 data, Java does not allow one time to shift all the bits of the left operand, that is, the right operand cannot be greater than 32. So back to the above sentence, which refers to the right operand of the low 5-bit, 5-bit binary represents the maximum value of 2^5-1, is 31, so take the right operand of the low 5-bit, is only to see the right operand of the binary low 5 bits, its value will not exceed 2^5, that is, int 32 bits. Therefore, the actual number of shift operator shifts is actually the number of right operand 2.


The understanding of the above paragraph is that the operator of the shift operator is the binary "bit", the int type is 32 bits that is 2 of the 5 power! If you move more than 32 digits, then the original number of information will be lost, so there is no point! So the above "only 5 low on the right side will be useful" is said: The shift operator on the right side of the number (into binary) of the low 5 bits is useful, that is
X < <y;

Refers to the low 5 bits of Y to be useful, i.e. no more than 32. And for the long type is the same truth!

So, if you shift to an int type, X < <y; When Y is less than 32 o'clock, the result of displacement is usually in our expectation, and if Y is greater than 32 o'clock, because the shift exceeds the range that int can represent, then the Y is converted into binary number, then the lower right side of the binary number is 5 bits, then the 5 bits into decimal, This decimal point is the number of bits to move to X.
For example:

int int a=140<<<< b));

The above two statements are executed as follows: First turn A into binary number: 10001100

Execute statements a << 34 to a left 32 bits, the first 34 into binary: 100010, the binary number to the right of 5 bits, that is 00010, the decimal number is 2, so it is actually a left shift two bits. Now, the people of the Earth will know the output of the above program is: 1000110000

//////////////////////////////////////////////////
The shift operator, like the bitwise operator, is the same as the bitwise operator, so the bitwise of the shift operator refers to bits. It includes the following types of:
Left Shift (<<): Moves the operand to the left of the operator to the left by the number of digits specified to the right of the operator. The moving rule is 0 in binary low-level.

Signed right Shift (>>): Moves the operand to the left of the operator to the right by the number of digits specified to the right of the operator. The rule of movement is that if the symbol of the operand is positive, the high position of the binary is 0, and if the symbol of the operand is negative, then the high position of the binary is 1.

Unsigned right Shift (>>>): Moves the operand to the left of the operator to the right by the number of digits specified to the right of the operator. The rule of movement is that whatever the symbol of the operand is exactly negative, it is 0 in the high position of the bits.
Note that there is no "unsigned Left Shift" (<<<) for the shift operator. Like bitwise operators, the shift operator can be used for integer types such as Byte, short, int, long, and string type char, but not for floating-point type float, double; of course, in Java5.0 and above, the shift operator can also be used for Byte, The wrapper class for short, int, long, char. We can follow the example of the bitwise operator to write a test program to verify that this is no longer an example.
Unlike bitwise operators, the shift operator does not have a short-circuiting problem.
Writing here will have to mention a topic that has been frequently tested in the interview question:

Please use the most efficient method to calculate 2 times 8 equals a few? The so-called most efficient, in fact, by the least, the simplest operation to obtain the desired result, and the shift is the computer is a fairly basic operation, with it to achieve the correct. The left shift "<<" shifts the operand to the left by one position, which is equivalent to multiplying the operand by 2, while 2*8= (2*2*2*2) shifts 2 to the left 3 times. So the most efficient way to calculate 2 times 8 is "2<<3".

Finally, let's consider a situation where the number of bits to shift is greater than the maximum number of digits that the operand's corresponding data type can represent, what will the result be? For example, 1<<35=?
Here are some other rules for shifting operations:
Byte, short, char are automatically converted to the int type before the shift operation, and then the operation is performed. Data of type Byte, short, int, char, after the shift operation, result in an int type. Long after the shift operation, the result is a long type.

In the left shift (<<) operation, if the number of bits to be shifted is greater than the maximum number of digits that can be represented by the corresponding data type of the operand, then the number of values of the remainder corresponding to the maximum number of digits that the type can represent will be shifted first, and the result will be the same as the number of operands.

such as 1<<35=1<&lt; (352) =1<<3=8. For signed right Shift (>>) operations and unsigned Right shift (>>>) operations, when the number of bits to be shifted is greater than the maximum number of digits that can be represented by the corresponding data type of the operand, then the required shift number is first calculated for the maximum number of digits that the type can represent. The value of the remainder corresponding to the operand is shifted, and the effect is unchanged.

such as 100>>35=100>> (352) =100>>3=12.

Other:

integer.tobinarystring methods for Java
 Public Static String tobinarystring (int  i)    // in binary (radix 2) unsigned integer returns the strings representation of an integer argument.      // If the argument is negative, the unsigned integer value is the parameter plus 2^32; otherwise equals the parameter.          System.out.println (integer.tobinarystring (-1));        System.out.println (Integer.tobinarystring (2));        System.out.println (integer.tobinarystring (1));
    输出:        11111111111111111111111111111111        11111111111111111111111111111110        1

Conclusion the output is the binary complement of the numbers. Why is it that a string of type integer is returned as a binary unsigned integer, why should we add 232 if the argument is negative?

Because the int in Java is signed, there are no positive or negative points in memory, only 0/1, the integers are in the complement.

Positive complement equals original code
The complement of a negative number equals the inverse of its absolute value +1, exactly equal to its own +2^32 (for a 4-byte integer)

-1The complement is 绝对值1 the anti-code (bitwise reverse) 11111111 11111111 11111111 11111110 and then+1
Equals11111111 11111111 11111111 11111111

This can be used to represent a number up to 1 for negative numbers, while a number with a maximum of 0 indicates a non-negative

10000000 00000000 00000000 00000000 => -214748364811111111 11111111 11111111 11111111 => -100000000 00000000 00000000 00000000 => 000000000 00000000 00000000 00000001 => 101111111 11111111 11111111 11111111 => 2147483647

The 负数+2^32 subsequent binary string, then, is the exact storage form of the negative memory

Reference, reprint:

Http://blog.sina.cn/dpool/blog/s/blog_6f436a590100nc37.html?vt=4

https://segmentfault.com/q/1010000002535852

Http://blog.sina.com.cn/s/blog_6ca0f5eb0102vlha.html

Http://www.cnblogs.com/bluestorm/p/5795461.html

Understanding Java Shift Operators

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.