Puzzle 31: the ghost of the loop

Source: Internet
Author: User

Please provide a Statement on I to convert the following loop into an infinite loop:

while (i != 0) {    i >>>= 1;}

In retrospect, >>>= is the value assignment operator corresponding to the unsigned right shift operator. 0 is moved from the left to the bit left by the shift operation, even if the negative number is shifted.

This loop is a little more complex than the previous three, because its loop body is not empty. In its cycle question, the value of I is replaced by the value after the right shift. To make the shift legal, I must be an integer (byte, Char, short, Int, or long ). The unsigned right shift operator moves 0 from the left, so it seems that the number of iterations executed in this loop is the same as the number of digits occupied by the largest integer type, that is, 64 times. If you place the following statement before the loop, this is indeed what will happen:

long i = -1; // -1L has all 64 bits set

How can you turn it into an infinite loop? The key to solving this puzzle is >>= is a composite value assignment operator. (The compound assignment operators include * =,/=, % =, + =,-=, <=, >>=, >>>=, <=, ^ =, and | = .) Unfortunately, they may automatically narrow the original type conversion [JLS 15.26.2], this type of conversion converts one numeric type to another type that lacks the presentation capability. Narrowing the original type conversion may result in loss of level information, or the numerical accuracy [JLS 5.1.3].

Let's be more specific. Suppose you put the following statement before the loop:

short i = -1;

Because the initial value of I (short) 0 xFFFF) is not 0, the loop is executed. When performing the shift operation, the first step is to upgrade I to the int type. All arithmetic operations will improve the short, byte, and char operations. This kind of improvement is a widening of the original type conversion, so no information will be lost. This promotion executes symbol extension, so the int value is 0 xffffffff. Then, this value shifts one bit to the right, but does not use symbol extension, so the int value 0x7fffffff is generated. Finally, the value is stored in I. To save the int value to the short variable, Java executes a terrible narrowing of the original type conversion, which directly blocks the high 16 bits. In this way, only (short) oxffff is left, and we are back at the beginning. The second and subsequent iterations of the loop are the same, so the loop will never end.

If you declare I as a short or BYTE variable and initialize it to any negative number, this behavior will also happen. If you declare I as a char, you will not be able to get an infinite loop, because Char is unsigned, so expanding the original type conversion before the shift will not execute symbol extension.

In short, do not use the composite value assignment operator on a variable of the short, byte, or char type. This expression executes mixed arithmetic operations, which can easily cause confusion. Even worse, their execution will implicitly implement a narrow transformation that will lose information, and the results will be disastrous.

The lesson for Language designers is that the language should not perform narrow conversions automatically. Another point worth noting is whether Java should prohibit the use of composite value assignment operators on short, byte, and char variables.

Puzzle 31: the ghost of the loop

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.