.net Int32 和 UInt32 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2307271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:57:00  来源:igfitidea点击:

What is the difference between Int32 and UInt32?

.netint32uint32

提问by Fitzchak Yitzchaki

What is the difference between Int32and UInt32?

Int32和 和有UInt32什么区别?

If they are the same with capacity range capabilities, the question is for what reason UInt32was created? When should I use UInt32instead of Int32?

如果它们与容量范围功能相同,那么问题是UInt32创建的原因是什么?我什么时候应该使用UInt32而不是Int32

回答by Otávio Décio

UInt32 does not allow for negative numbers. From MSDN:

UInt32 不允许负数。从MSDN

The UInt32 value type represents unsigned integers with values ranging from 0 to 4,294,967,295.

UInt32 值类型表示值范围从 0 到 4,294,967,295 的无符号整数。

回答by Randy Minder

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

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

This articlemight help you.

这篇文章或许能帮到你。

回答by fen

uint32is an unsigned integer with 32 bit which means that you can represent 2^32 numbers (0-4294967295).

uint32是一个 32 位的无符号整数,这意味着您可以表示 2^32 个数字 (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.

然而,为了表示负数,32位中保留一位来表示正数或负数。这给你留下了 2^31 个可能的负数和正数。结果范围是 -2147483648 到 2147483647(正范围包括值 0,因此只有 2147483647)。这种表示被称为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 int32is not possible since int32can't hold the range of uint32and vice versa.

您应该为定义不能为负数的数字选择无符号,因为它为您提供了更广泛的范围,但您应该记住,从和到的转换int32是不可能的,因为int32不能保持范围,uint32反之亦然。

回答by Matt H

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

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

回答by Robby Wasabi

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:

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

  • 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.
  • 255 是0xff,-1 也是0xff
  • 将 -1 解释为“零的左边一个”,而 +1 是“零的右边一个”。
  • -1 + -1 = -2,即“零左边两个”,也就是0xfe。好吧,如果我们将其转换为无符号数学,则它变为 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.

当编译器必须通过符号扩展从较小的大小转换为较大的大小时,签名变得很重要。所以在这种情况下,重要的是指明一个类型,这样编译器才能做正确的事情。