C # confusing 27: unpredictable I value

Source: Internet
Author: User

Puzzle 27: unpredictable I value
Your task is still to point out thisProgramWhat will be printed.
Class shifty
{
Static void main ()
{
Int I = 0;
While (-1 <I! = 0)
I ++;
System. Console. writeline (I );
}
}

Confusing 27: unpredictable I value
Constant-1 is the int value (0 xffffffff) of all 32 bits ). The Left shift operator moves 0 to the right trim bit left blank by the shift, so the expression (-1 <I) sets the rightmost I bit to 0, and keep the remaining 32-i bits as 1. Obviously, this loop completes 32 iterations, because (-1 <I) is not equal to 0 for any I smaller than 32. You may expect to return false when I is equal to 32 to enable 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) is equal to-1 instead of 0,Because the shift operator only uses the lower five digits of its right operand as the shift length. Or 6 lower bits, if the left operand is a long value [C # Language Specification 7.8]. This rule applies to all two shift operators: <and>. The shift length is always between 0 and 31. If the left operand is of the long type, it is between 0 and 63. This length is equal to 32. If the left operand is of the long type, it is equal to 64. If you try to shift a 32-bit int value or a 64-bit long value, you can only return this value. Without any shift length, an int value can discard all its 32 bits, or a long value can discard all its 64 bits.
Fortunately, there is a very easy way to fix this issue. Instead of letting-1 repeatedly shift different shift lengths, we save the results of the previous shift operation and let it shift one bit left during each iteration. The program of the following version can print the expected 32:
Class shifty
{
Static void main ()
{
Int distance = 0;
For (INT val =-1; Val! = 0; Val <= 1)
Distance ++;
System. Console. writeline (distance );
}
}
This corrected procedure illustrates a general principle: If possible, the shift length should be a constant. If you keep the shift length close, you can set the value to more than 31, or if the left operand is of the long type, the possibility of exceeding 63 will be greatly reduced. Of course, it is not always possible to use the shift length of a constant. When you must use a very large shift length, make sure that your program can handle this situation that is prone to problems, or you will not encounter this situation at all.
The behavior of the shift operator mentioned above has another shocking result. Many programmers want the right shift operator with negative shift length to act as the left shift operator, and vice versa. But this is not the case. The right shift operator always acts as the right shift, and the left shift operator always acts as the left shift. The negative shift length is converted to the positive shift length by retaining only five digits while the other digits. If the left operand is of the long type, the negative shift length is kept as low as six digits. Therefore, if you want to shift an int value to the left and its shift length is-1, the shift effect is that it is shifted to 31 places.
In short, the shift length is equal to 32, or if the left operand is of the long type, it is equal to 64. Therefore, it is impossible to remove all bits of a value using any shift operator or shift length. At the same time, we cannot use the right shift operator to perform the Left shift operation, and vice versa. If possible, use the shift length of a constant. If the shift length cannot be set to a constant, be careful.
The language designer may consider limiting the shift length to the range of the type length from 0 to bit, and modifying the semantics of the shift length to the type length, so that it returns 0. Although this can avoid confusion in this puzzle, it may bring negative execution results, because the semantics of the C # shift operator is exactly the shift instruction semantics on many processors.

C # directory

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.