Overflowing of Unsigned Int

前端 未结 3 1399
死守一世寂寞
死守一世寂寞 2020-11-28 12:43

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

3条回答
  •  半阙折子戏
    2020-11-28 13:21

    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;
    

提交回复
热议问题