The following code:
public class Example027 {public static void main (string[] args) {int i = 0;while ( -1 << ~ = 0) {i++;} System.out.println (i);}}
Result Description:
put the above program into eclipse, and the output line will prompt "unreachable code". That is, the while loop is a dead loop and cannot be exited.
Results Analysis:
Java uses a 2 complement binary arithmetic operation, so that 1 of any signed integer type (byte, short, int, or long) is represented by all bits 1.
The constant-1 is the int value (0xFFFFFFFF) that all 32 bits are set. the left shift operator moves 0 into the lowest bit on the right that is vacated by the shift, so the expression ( -1 << i) sets the rightmost bit of I to 0 and keeps the remaining 32-i bits at 1. Obviously, this loop will complete 32 iterations, because -1 << I are not equal to 0 for any I less than 32. You may expect the terminating condition test to return false at I equals 32 o'clock, which causes the program to print 32, but it does not print 32. In fact, it does not print anything, but enters an infinite loop.
The problem is that ( -1 << 32) equals-1 instead of 0, because the shift operator uses only the lower 5 bits of its right operand as the shift length. or a low 6-bit, if its left operand is a long class number.
This rule acts on all three shift operators: <<, >>, and >>>. The shift length is always between 0 and 31, or between 0 and 63 if the left operand is of type long. This length is for 32, and if the left operand is a long type, then the remainder is 64. If you attempt to shift 32 bits to an int value, or if you shift a long value to 64 bits, you can only return the value of that value itself. There is no shift length that allows an int value to discard all of its 32 bits, or a long value to discard all its 64 bits.
One possible program improvement scenario is that we do not allow 1 to repeat the different shift lengths, but instead save the results of the previous shift operation and let it move 1 bits to the left at each iteration. The following version of the program can print out the 32 we expect:
private static void Shift () {int distance = 0;for (int val =-1; Val! = 0; Val <<= 1)//Shift 32 times after left, var = = 0distance++; System.out.println (distance);}
Many programmers want the right-shift operator with a negative shift length to act as a left-shift operator and vice versa. But the sentiment
This is not the case. The right-shift operator always moves to the right, and the left-shift operator always acts as a left-shift. The negative shift length is converted to a positive shift length by preserving only the lower 5 bits and rejecting the other bits (if the left operand is a long type, the lower 6 bits are retained). Therefore, if you want to move an int value to the left and its shift length is-1, then the effect of the shift is that it is shifted 31 bits to the left.
The shift length is 32, or if the left operand is a long type, 64 is the remainder. Therefore, it is not possible to remove all bits of a numeric value by using any shift operator and shift length. At the same time, we cannot use the right-shift operator to perform a left-shift operation, or vice versa. If possible, use a constant shift length and be careful if the shift length cannot be set to constant.
Source code Address: Https://github.com/rocwinger/java-disabuse
This article from "Winger" blog, declined reprint!
Java FAQ shift Operator rule