Overflowing of Unsigned Int
Asked Answered
A

3

21

What will the unsigned int contain when I overflow it? To be specific, I want to do a multiplication with two unsigned ints: what will be in the unsigned int after the multiplication is finished?

unsigned int someint = 253473829*13482018273;
Ariellearies answered 8/2, 2012 at 13:6 Comment(4)
This seems to be a duplicate of #199833Fascism
Why not try it and see what you get? In general, when an unsigned int overflows, it rolls over to zero. So UINT_MAX + 5 rolls over and becomes 4.Choong
It would be the difference between the max uint value and the value of what would have been the overflow value. Lets make it simple. Lets say the max uint is 5. You want to add 2 * 4 so this makes the final value 3 instead of 8.Homeless
#989088Dupion
C
29

unsigned numbers can't overflow, but instead wrap around using the properties of modulo.

For instance, when unsigned int is 32 bits, the result would be: (a * b) mod 2^32.


As CharlesBailey pointed out, 253473829*13482018273 may use signed multiplication before being converted, and so you should be explicit about unsigned before the multiplication:

unsigned int someint = 253473829U * 13482018273U;
Commentary answered 8/2, 2012 at 13:14 Comment(11)
is that a part of a standard?Luanaluanda
@Zhenya Yes, in both C and C++.Commentary
@Zhenya - Does it matter? The answer is 100% correct. Its a more technical way of saying UINT_MAX + 5 is 4. This likely would remain true in both .NET languages and Java. At least in the case of .NET NaN is limited to types like double where the value ( most of the time ) is not represented exactly.Homeless
The same happens with other built-in data types , right? , ThanksLongboat
@Ramhound: Of course it matters. If the standard didn't define this behaviour (like it doesn't for signed integer types), then you couldn't rely on it.Nelle
@Mr.Anubis: No, only unsigned integer types. Signed and floating point overflows give undefined behaviour.Nelle
This answer is not necessarily relevant. Depending on the compiler limits, the expression 253473829*13482018273 may use signed integer arithmetic which may overflow before the result is converted to unsigned int.Obsessive
@CharlesBailey: The question asks about multiplying two unsigned ints, even if the example code shows something else.Nelle
@MikeSeymour you mean all signed types and including floating types, and excluding all unsigned types, right?, ThanksLongboat
@Mr.Anubis: Yes; the behaviour is defined for unsigned integer types only.Nelle
Except with regard to promotion rules, unsigned values mostly do not behave as numbers, but rather members of an algebraic ring (typically Z256, Z65536, Z4294967296, or Z18446744073709551616). X-Y is the value which, when added to Y, will yield X. Some promotion rules are correct when applied to members of an algebraic ring (e.g. adding an unsigned value to an int which is no larger correctly moves around the ring), though some are not. In a new language design, it might be helpful to have separate types for numbers and algebraic rings; both have their uses, independent of signedness.Aide
A
7

Unsigned integer overflow, unlike its signed counterpart, exhibits well-defined behaviour.

Values basically "wrap" around. It's safe and commonly used for counting down, or hashing/mod functions.

Annoying answered 8/2, 2012 at 13:17 Comment(1)
I meant that as a comparison to try explain it by relating to something similar. I did qualify my statement later with the wrap around bit. Ah technicalities.Annoying
A
-3

It probably depends a bit on your compiler. I had errors like this years ago, and sometimes you would get runtime error, other times it would basically "wrap" back to a really small number that would result from chopping off the highest level bits and leaving the remainder, i.e if it's a 32 bit unsigned int, and the result of your multiplication would be a 34 bit number, it would chop off the high order 2 bits and give you the remainder. You would probably have to try it on your compiler to see exactly what you get, which may not be the same thing you would get with a different compiler, especially if the overflow happens in the middle of an expression where the end result is within the range of an unsigned int.

Academician answered 8/2, 2012 at 13:22 Comment(1)
Unsigned overflow does not depend on the compiler, it is standardized to have wrap-around semantics. It is only signed overflow that leads to undefined values, and may thus depend on the compiler.Wolver

© 2022 - 2024 — McMap. All rights reserved.