Java Reference
In-Depth Information
< Day Day Up >
Puzzle 31: Ghost of Looper
Provide a declaration for i that turns this loop into an infinite loop:
while (i != 0)
i >>>= 1;
Recall that >>>= is the assignment operator corresponding to the unsigned right-shift operator.
Zeros are shifted in from the left to fill bits vacated by the shift, even if the value being shifted is
negative.
Solution 31: Ghost of Looper
This looper is a bit more complex than the three that preceded it, as the loop has a nonempty body.
In the body, the value of i is replaced by its value shifted right by one bit position. For the shift to
be legal, i must be of an integral type ( byte , char , short , int , or long ). The unsigned right-shift
operator shifts zeros in from the left, so it might seem that the loop could perform only as many
iterations as the number of bits in the largest integral type, which is 64. This is indeed what would
happen if you preceded the loop with this declaration:
long i = -1; // -1L has all 64 bits set
 
 
Search WWH ::




Custom Search