What happens if I assign a negative value to an unsigned variable?
Solution 1
For the official answer - Section 4.7 conv.integral
"If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2n where
n
is the number of bits used to represent the unsigned type). [ Note: In a two’s complement representation, this conversion is conceptual and there is no change in the bit pattern (if there is no truncation). —end note ]
This essentially means that if the underlying architecture stores in a method that is not Two's Complement (like Signed Magnitude, or One's Complement), that the conversion to unsigned must behave as if it was Two's Complement.
Solution 2
It will assign the bit pattern representing -5 (in 2's complement) to the unsigned int. Which will be a large unsigned value. For 32 bit ints this will be 2^32 - 5 or 4294967291
Solution 3
You're right, the signed integer is stored in 2's complement form, and the unsigned integer is stored in the unsigned binary representation. C (and C++) doesn't distinguish between the two, so the value you end up with is simply the unsigned binary value of the 2's complement binary representation.
Solution 4
It will show as a positive integer of value of max unsigned integer - 4 (value depends on computer architecture and compiler).
BTW
You can check this by writing a simple C++ "hello world" type program and see for yourself
Solution 5
Yes, you're correct. The actual value assigned is something like all bits set except the third. -1 is all bits set (hex: 0xFFFFFFFF), -2 is all bits except the first and so on. What you would see is probably the hex value 0xFFFFFFFB which in decimal corresponds to 4294967291.
ckv
Updated on April 28, 2020Comments
-
ckv about 4 years
I was curious to know what would happen if I assign a negative value to an unsigned variable.
The code will look somewhat like this.
unsigned int nVal = 0; nVal = -5;
It didn't give me any compiler error. When I ran the program the
nVal
was assigned a strange value! Could it be that some 2's complement value gets assigned tonVal
? -
ckv about 14 yearsI wrote and checked it thats why i asked the question but i didnt know how the compiler arrived at that positive value. Thanks
-
GManNickG about 14 yearsIt may not be stored in 2's compliment.
-
GManNickG about 14 yearsBit's have nothing to do with it.
-
GManNickG about 14 yearsBit's have nothing to do with it, integer representation isn't specified.
-
Martin about 14 yearsYour answer is correct, stringent, to the point and something I would never use in class.
-
cynistersix over 12 yearssee my answer for the 2's complement of -5. I don't think you did your math correctly on the binary values here.
-
GManNickG over 11 years@BenVoigt: Fair enough, I meant it had nothing to do with how bits are interpreted. (That is, the "bits" in the quoted part is just shorthand for
ceil(log_2(x))
.) -
NullUserException over 11 years@GManNickG Bit's (as in, belongs to the bit)? 2's Compliment (that's very nice of you)? GAAAAAAAAAAAAAH!
-
GManNickG over 11 years@NullUserException: Haha, I know. Writing "*'s" in place of just "*s" is a terrible habit I've had for a while. As for compliment instead of complement, that's just pure tomfoolery. :)
-
David Rodríguez - dribeas about 11 yearsWhat does the least unsigned integer congruent to the source integer mean?
-
JoeQuery over 10 years@DavidRodríguez-dribeas As an example, 5 and 3 are "congruent mod 2" since 5%2 and 3%2 are both 1.
-
JeremyF over 9 yearsWhat does it mean if something is "stored in 2's?" @GManNickG
-
GManNickG over 9 years@JeremyF: Not "2's", "2's compliment". It's a Google-able term, and a way of representing signed integers.
-
dystopiandev over 7 yearsSimplicity is key. This answer has that. (2^32 - 5) explains this behaviour better than quoting the documentation.
-
Ben Jones almost 6 yearsUnfortunately with C++, writing programs to test behavior is not always a good idea. For instance, if one tried to test what happens in the case of signed overflow, it will lead to undefined behavior, which is not guaranteed to be the same on every machine/compiler.
-
Alexey Kruglov over 3 yearsWhich versions of C++ standard does it relate to? All?
-
Eyal over 2 yearsSo is it true that (uint)((int)a + (int)b) might be undefined because a and b could overflow but (uint)a + (uint)b is well-defined because overflow is allowed for uint. And both will give identical results for all well-defined sums?