Int32 和 UInt32 有什么区别?
Int32
和 UInt32
之间有什么区别?
如果它们与容量范围功能相同,那么问题是创建 UInt32 的原因是什么?什么时候应该使用 UInt32
而不是 Int32
?
What is the difference between Int32
and UInt32
?
If they are the same with capacity range capabilities, the question is for what reason UInt32
was created? When should I use UInt32
instead of Int32
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
UInt32 不允许负数。来自 MSDN:
UInt32 值类型表示值范围为无符号整数从 0 到 2 的 32 次方或 2**32(等于 4,294,967,295)。
UInt32 does not allow for negative numbers. From MSDN:
The UInt32 value type represents unsigned integers with values ranging from 0 to 2 to the power of 32 or 2**32 (which equals to 4,294,967,295).
整数为 -2147483648 到 2147483647,无符号整数为 0 到 4294967295。
这篇文章可能帮助你。
An integer is -2147483648 to 2147483647 and an unsigned integer is 0 to 4294967295.
This article might help you.
uint32
是一个 32 位无符号整数,这意味着可以表示 2^32 个数字 (0-4294967295)。然而,为了表示负数,32位中的一位被保留来指示正数或负数。这给你留下了 2^31 个可能的负数和正数。结果范围是 -2147483648 到 2147483647(正范围包括值 0,因此只有 2147483647)。这种表示形式称为
int32
。对于定义上不能为负数的数字,您应该选择无符号,因为它为您提供了更广泛的范围,但您应该记住,从和到
int32
的转换是不可能的,因为int32< /code> 无法容纳
uint32
的范围,反之亦然。uint32
is an unsigned integer with 32 bit which means that you can represent 2^32 numbers (0-4294967295).However, in order to represent negative numbers, one bit of the 32 bits is reserved to indicate positive or negative number. this leaves you with 2^31 possible numbers in the negative and also in the positive. The resulting range is -2147483648 to 2147483647 (positive range includes the value 0, hence only 2147483647). This representation is called
int32
.You should choose unsigned for numbers which can't get negative by definition since it offers you a wider range, but you should keep in mind that converting from and to
int32
is not possible sinceint32
can't hold the range ofuint32
and vice versa.uint32
是无符号 32 位整数。它不能用来表示负数,但可以容纳更大的正数。uint32
is unsigned 32-bit integer. It can't be used to represent negative numbers but can hold greater positive numbers.它们之间没有区别。
区别在于它的表示方式,例如通过打印到终端。
例如,为了简单起见,坚持使用 8 位值:
0xff
,-1 也是0xff
。0xfe
。好吧,如果我们将其转换为无符号数学,那么它就会变成 0xff + 0xff = 0xfe。所以你看,有符号和无符号之间没有区别,只是我们最终如何表示它们才产生区别,在这种情况下,类型指示了它的表示方式。
当编译器必须通过符号扩展将较小的大小转换为较大的大小时,符号性变得很重要。因此在这种情况下,指示类型非常重要,以便编译器可以做正确的事情。
There is no difference between them.
The difference is how it is represented, such as via printing to terminal.
For example, and sticking with 8 bit values for simplicity:
0xff
, and -1 is also0xff
.0xfe
. Well, if we convert that to unsigned math, then, it becomes 0xff + 0xff = 0xfe.So you see, there is no difference between signed and unsigned, it's only how we represent them in the end that makes the difference, and in this case, the type indicates how it is represented.
Signedness becomes important when the compiler has to cast from a smaller size to a bigger via sign extension. So in this case, it's important to indicate a type so that the compiler can do the right thing.