无符号整数溢出 [英] Overflowing of Unsigned Int
问题描述
溢出时, unsigned int
包含什么?具体来说,我想和两个 unsigned int
做一个乘法,想知道 unsigned int $ c $在乘法完成之后,c>>
What will the unsigned int
contain when I overflow it? To be specific, I want to do a multiplication with two unsigned int
s 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屋!