无符号整数溢出 [英] Overflowing of Unsigned Int

查看:192
本文介绍了无符号整数溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

溢出时, unsigned int 包含什么?具体来说,我想和两个 unsigned int 做一个乘法,想知道 unsigned int >

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

unsigned int someint = 253473829*13482018273;


推荐答案

无符号数字不能溢出,而是使用模的属性包围。

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

例如,当 unsigned int 为32位时,结果为: a * b)mod 2 ^ 32

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

正如CharlesBailey指出的, 253473829 * 13482018273 可以在转换之前使用有符号乘法,因此您应该在乘法之前明确 unsigned

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;

这篇关于无符号整数溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆