Int32 和 UInt32 有什么区别?

发布于 2024-08-22 04:58:28 字数 153 浏览 13 评论 0原文

Int32UInt32 之间有什么区别?

如果它们与容量范围功能相同,那么问题是创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

内心旳酸楚 2024-08-29 04:58:28

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).

初与友歌 2024-08-29 04:58:28

整数为 -2147483648 到 2147483647,无符号整数为 0 到 4294967295。

这篇文章可能帮助你。

An integer is -2147483648 to 2147483647 and an unsigned integer is 0 to 4294967295.

This article might help you.

残疾 2024-08-29 04:58:28

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 since int32 can't hold the range of uint32 and vice versa.

深府石板幽径 2024-08-29 04:58:28

uint32 是无符号 32 位整数。它不能用来表示负数,但可以容纳更大的正数。

uint32 is unsigned 32-bit integer. It can't be used to represent negative numbers but can hold greater positive numbers.

暮倦 2024-08-29 04:58:28

它们之间没有区别。

区别在于它的表示方式,例如通过打印到终端。

例如,为了简单起见,坚持使用 8 位值:

  • 255 是 0xff,-1 也是 0xff
  • 将 -1 解释为“零左侧的一”,而 +1 则为“零右侧的一”。
  • -1 + -1 = -2,即“零左边的二”,也就是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:

  • 255 is 0xff, and -1 is also 0xff.
  • Interpret -1 as, "One to the left of zero", whereas +1 is, "One to the right of zero".
  • -1 + -1 = -2, which is, "Two to the left of zero", which is also 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文