Overflowing of Unsigned Int

21,433

Solution 1

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;

Solution 2

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.

Share:
21,433
GILGAMESH
Author by

GILGAMESH

Updated on April 12, 2020

Comments

  • GILGAMESH
    GILGAMESH about 4 years

    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;
    
  • ev-br
    ev-br over 12 years
    is that a part of a standard?
  • Pubby
    Pubby over 12 years
    @Zhenya Yes, in both C and C++.
  • Security Hound
    Security Hound over 12 years
    @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.
  • evandrix
    evandrix over 12 years
    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.
  • Mr.Anubis
    Mr.Anubis over 12 years
    The same happens with other built-in data types , right? , Thanks
  • Mike Seymour
    Mike Seymour over 12 years
    @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.
  • Mike Seymour
    Mike Seymour over 12 years
    @Mr.Anubis: No, only unsigned integer types. Signed and floating point overflows give undefined behaviour.
  • CB Bailey
    CB Bailey over 12 years
    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.
  • Mike Seymour
    Mike Seymour over 12 years
    @CharlesBailey: The question asks about multiplying two unsigned ints, even if the example code shows something else.
  • Mr.Anubis
    Mr.Anubis over 12 years
    @MikeSeymour you mean all signed types and including floating types, and excluding all unsigned types, right?, Thanks
  • Mike Seymour
    Mike Seymour over 12 years
    @Mr.Anubis: Yes; the behaviour is defined for unsigned integer types only.
  • Mr.Anubis
    Mr.Anubis over 12 years
    @MikeSeymour Thanks a lot :)))
  • supercat
    supercat over 10 years
    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.
  • cmaster - reinstate monica
    cmaster - reinstate monica about 7 years
    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.