Positive solution "x = -1;"
-1 exceeds the expression range of the unsigned integer x, according to the standard (c++11 section 4.7.2), 1 will be converted to 2^n-1 (n is the number of bits of x).
Non-positive solution "x = 0xffffffff;"
This applies only to the 32bit integer type.
Non-positive solution "x = ~0;"
This is a lot of books as a "standard answer" answer is also wrong. Three negative representations of the original code/inverse code/complement are supported in the C + + standard, and the correct result is obtained only in the complement environment. (for example, under anti-code, ~0 will get -0,-0 to unsigned still 0)
So x = ~0u ?
is still wrong. According to the standard (c++11 section 2.14.2.2), the literal 0u is unsigned int. Then the inverse is Uint_max, if x can represent the maximum value is not Uint_max then the result is wrong.
How to add an unsigned integer all data position to 1