简体   繁体   English

“int”和“uint”/“long”和“ulong”有什么区别?

[英]What is the difference between “int” and “uint” / “long” and “ulong”?

I know about int and long (32-bit and 64-bit numbers), but what are uint and ulong ?我知道intlong (32 位和 64 位数字),但是uintulong是什么?

The primitive data types prefixed with "u" are unsigned versions with the same bit sizes.以“u”为前缀的原始数据类型是具有相同位大小的无符号版本。 Effectively, this means they cannot store negative numbers, but on the other hand they can store positive numbers twice as large as their signed counterparts.实际上,这意味着它们不能存储负数,但另一方面,它们可以存储两倍于有符号数的正数。 The signed counterparts do not have "u" prefixed.签名的副本没有“u”前缀。

The limits for int (32 bit) are: int(32 位)的限制是:

int: –2147483648 to 2147483647 
uint: 0 to 4294967295 

And for long (64 bit):并且长期(64位):

long: -9223372036854775808 to 9223372036854775807
ulong: 0 to 18446744073709551615

uint and ulong are the unsigned versions of int and long . uintulongintlong的无符号版本。 That means they can't be negative.这意味着它们不能是负面的。 Instead they have a larger maximum value.相反,它们具有更大的最大值。

Type    Min                           Max                           CLS-compliant
int     -2,147,483,648                2,147,483,647                 Yes
uint    0                             4,294,967,295                 No
long    –9,223,372,036,854,775,808    9,223,372,036,854,775,807     Yes
ulong   0                             18,446,744,073,709,551,615    No

To write a literal unsigned int in your source code you can use the suffix u or U for example 123U .要在源代码中编写文字 unsigned int,您可以使用后缀uU例如123U

You should not use uint and ulong in your public interface if you wish to be CLS-Compliant .如果您希望 符合 CLS 标准,则不应在公共界面中使用 uint 和 ulong 。

Read the documentation for more information:阅读文档了解更多信息:

By the way, there is also short and ushort and byte and sbyte .顺便说一下,还有shortushort以及bytesbyte

The difference is that the uint and ulong are unsigned data types, meaning the range is different: They do not accept negative values:区别在于uintulong是无符号数据类型,意味着范围不同:它们不接受负值:

int range: -2,147,483,648 to 2,147,483,647
uint range: 0 to 4,294,967,295

long range: –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
ulong range: 0 to 18,446,744,073,709,551,615

u means unsigned , so ulong is a large number without sign. u表示unsigned ,所以ulong是一个没有符号的大数。 You can store a bigger value in ulong than long , but no negative numbers allowed.您可以在ulong存储比long更大的值,但不允许使用负数。

A long value is stored in 64-bit,with its first digit to show if it's a positive/negative number. long值以 64 位存储,第一个数字表示它是正数还是负数。 while ulong is also 64-bit, with all 64 bit to store the number.ulong也是 64 位的,所有 64 位都用来存储数字。 so the maximum of ulong is 2(64)-1, while long is 2(63)-1.所以ulong的最大值是2(64)-1,而long是2(63)-1。

It's been a while since I C++'d but these answers are off a bit.自从我使用 C++ 以来已经有一段时间了,但这些答案有点偏离。

As far as the size goes, 'int' isn't anything.就大小而言,'int' 不算什么。 It's a notional value of a standard integer;它是一个标准整数的概念值; assumed to be fast for purposes of things like iteration.假设为了迭代之类的目的是快速的。 It doesn't have a preset size.它没有预设大小。

So, the answers are correct with respect to the differences between int and uint, but are incorrect when they talk about "how large they are" or what their range is.因此,对于 int 和 uint 之间的差异,答案是正确的,但是当他们谈论“它们有多大”或它们的范围是什么时,答案是不正确的。 That size is undefined, or more accurately, it will change with the compiler and platform.这个大小是不确定的,或者更准确地说,它会随着编译器和平台而改变。

It's never polite to discuss the size of your bits in public.在公共场合讨论你的比特的大小从来都不是礼貌的。

When you compile a program, int does have a size, as you've taken the abstract C/C++ and turned it into concrete machine code.当你编译一个程序时, int确实有一个大小,因为你已经把抽象的 C/C++ 变成了具体的机器代码。

So, TODAY, practically speaking with most common compilers, they are correct.所以,今天,实际上用最常见的编译器来说,它们是正确的。 But do not assume this.但不要假设这一点。

Specifically: if you're writing a 32 bit program, int will be one thing, 64 bit, it can be different, and 16 bit is different.具体来说:如果您正在编写 32 位程序,则 int 将是一回事,64 位可以不同,而 16 位则不同。 I've gone through all three and briefly looked at 6502 shudder我已经完成了所有三个并简要地看了看 6502不寒而栗

A brief google search shows this: https://www.tutorialspoint.com/cprogramming/c_data_types.htm This is also good info:https://docs.oracle.com/cd/E19620-01/805-3024/lp64-1/index.html一个简短的谷歌搜索显示: https : //www.tutorialspoint.com/cprogramming/c_data_types.htm这也是很好的信息:https ://docs.oracle.com/cd/E19620-01/805-3024/lp64- 1/index.html

use int if you really don't care how large your bits are;如果您真的不关心您的位有多大,请使用 int; it can change.它可以改变。

Use size_t and ssize_t if you want to know how large something is.如果您想知道某物有多大,请使用 size_t 和 ssize_t。

If you're reading or writing binary data, don't use int.如果您正在读取或写入二进制数据,请不要使用 int。 Use a (usually platform/source dependent) specific keyword.使用(通常依赖于平台/源)特定的关键字。 WinSDK has plenty of good, maintainable examples of this. WinSDK 有很多很好的、可维护的例子。 Other platforms do too.其他平台也是如此。

I've spent a LOT of time going through code from people that "SMH" at the idea that this is all just academic/pedantic.我花了很多时间浏览那些“SMH”的人的代码,认为这只是学术/迂腐。 These ate the people that write unmaintainable code.这些吞噬了编写不可维护代码的人。 Sure, it's easy to use type 'int' and use it without all the extra darn typing.当然,使用类型“int”很容易,并且无需额外的打字就可以使用它。 It's a lot of work to figure out what they really meant, and a bit mind-numbing.弄清楚它们的真正含义需要做很多工作,而且有点让人麻木。

It's crappy coding when you mix int.当你混合 int 时,这是糟糕的编码。

use int and uint when you just want a fast integer and don't care about the range (other than signed/unsigned).当您只想要一个快速整数而不关心范围(有符号/无符号除外)时,请使用 int 和 uint 。

Based on the other answers here and a little review you can understand it this way: unsigned is in reference to the assignment of a negative or positive explicit assignment (think the "-" in -1) and the inability to have negative versions of said numbers.根据此处的其他答案和一些评论,您可以这样理解:无符号是指负或正显式分配的分配(想想-1中的“-”)并且无法拥有所述的否定版本数字。

And because of this capacity on the negative end being removed as an option they instead allocated that capacity to the positive end hence the doubling of the positive valuation's maximum value.由于负端的这种容量作为一种选择被删除,他们改为将该容量分配给正端,因此正估值的最大值翻了一番。 So instead of the bit range being split along positive and negative valuations, they are instead for ushort, uint, along, etc allocated to the positive end of the valuation.因此,不是将位范围沿正估值和负估值分开,而是将它们分配给估值的正端的 ushort、uint、沿等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM