C # confusing 33: The loop operator encounters a werewolf

Source: Internet
Author: User

Puzzle 33: The loop encountered a werewolf
Provide an I declaration to convert the following loop into an infinite loop.
While (I! = 0 & I =-I)
{
}

Confused 33: The loop encountered a werewolf
This is still a loop. In the Boolean expression (I! In = 0 & I =-I), The unary minus sign operator acts on I, meaning that it must be of a number type: the unary minus sign operator is invalid for a non-numeric pre-defined type operand. Therefore, we need to find a non-0 numeric value, which is equal to its own negative value. Nan cannot satisfy this attribute because it is not equal to any value. Therefore, I must represent an actual number. Are you sure no numbers meet this attribute?
Well, no real number has this attribute, but no C # numeric type can perfectly model the real number. A floating point value is represented by a signed bit, a valid number commonly referred to as mantissa, and an index. Except for 0, there is no floating point number equal to the value after its symbol bit is reversed. Therefore, the I type must be an integer.
The signed integer type uses the 2's complement arithmetic operation: in order to obtain a negative value of a value, we need to reverse each bit of the value and Add 1 to obtain the result. A major advantage of 2's complement arithmetic operation is that 0 has a unique representation. If you want to take a negative value for the int value 0, it will get 0 xffffffff + 1, it is still 0. However, this also has a corresponding disadvantage. There are an even number of int values in total-to be exact, there are 232, one of which is used to represent 0, and the remaining odd number is used to represent positive integers and negative integers, this means that the number of positive and negative int values must be not equal. In other words, this implies at least one int value, and its negative value cannot be correctly expressed as an int value.
In fact, there is exactly one such int value, which is int. minvalue, that is,-231. Its hexadecimal format is 0x80000000. Its symbol bit is 1, and all other bits are 0. If we take a negative value for this value, we will get 0x7fffff + 1, that is, 0x80000000, that is, Int. minvalue! In other words, Int. minvalue is its own negative value, Long. minvalue is also the same [C # Language Specification 7.6.2]. The negative values of these two values will overflow, but C # ignores the overflow in the unchecked context. The results have been clarified, even if they are not always what you expect.
The following statement makes the Boolean expression (I! = 0 & I =-I) The calculation result is true, so that the loop is infinite:
Int I = int. minvalue;
This can also be:
Long I = long. minvalue;
If you are familiar with modulo operations, it is necessary to point out that you can also use algebra to solve this puzzle. The Int arithmetic operation of C # is the actual arithmetic operation modulo 232. Therefore, this puzzle requires a non-zero solution for this linear equality:
I have-I (mod 232)
After I is added to both sides of the constant equation, we can get:
2I limit 0 (mod 232)
The non-zero solution for such full equality is I = 231. Although this value cannot be expressed as an int, It is equal to-231, that is, it is equal to int. minvalue.
In short, C # uses the 2's complement arithmetic operation, which is asymmetric. For each signed integer type (INT, long, sbyte, and short), the negative value is always one more than the positive value, this extra value is always the minimum value that can be expressed by this type. Taking a negative value for int. minvalue does not change its value, as does long. minvalue. Take a negative value for short. minvalue and convert the generated int value back to short. The returned value is also the initial value (short. minvalue ). Similar results are also produced for sbyte. minvalue. More generally, Always Be careful with overflow: Like a werewolf, It is a killer.
The lessons for Language designers are the same as those in puzzle 26. Provide language-level support for some integer arithmetic operations that do not quietly overflow.
(Note: overflow check will be performed in the C # checked context [C # Language Specification 7.5.12])

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.