[C/C ++]
Positive Solution:
X =-1;
-1 is out of the expression range of the unsigned integer x. According to the standard (C ++ 11 section 4.7.2),-1 is converted to 2 ^ N-1 (n is the number of bits of x ).
Solution:
X = 0 xFFFFFFFF;
This is only applicable to 32bit integers.
Solution:
X = ~ 0;
This is also the answer that many books recognize as a "standard answer. The C/C ++ standard supports three negative representation forms: original code, reverse code, and complement code. The correct result is obtained only in the complement environment. (For example, in the back code ,~ 0 will get-0,-0 to unsigned, still 0)
Then x = ~ What about 0u?
Still wrong. According to the standard (C ++ 11 section 2.14.2.2), the literal 0u is an unsigned int. UINT_MAX is obtained after the inversion. If the maximum value that x can represent is not UINT_MAX, the result is wrong.
In C, if an integer occupies four bytes in memory, which value range of the unsigned integer data is used?
2 ^ 31-1
In C, you can assign a negative value to an unsigned integer variable.
Yes, but the result will go wrong. The maximum digit of the negative number in the computer is 1, which is used as a symbol sign (positive number is 0). However, if you assign it to an unsigned integer variable, the flag of the highest bit is a numerical bit, which is regarded as a numerical value during calculation.