简体   繁体   English

32位系统上的长整数是否可以工作?

[英]Will a long-integer work on a 32 bit system?

If I understand it right an int-variable is saving in 32 bit, restricting it to -2 billion to 2 billion something. 如果我理解它是正确的,int-variable以32位保存,将其限制为20亿到20亿。 However if I use a long-variable it will save in 64 bit allowing a lot more numbers to be stored. 但是,如果我使用长变量,它将保存为64位,允许存储更多数字。 I'm sitting on a 64 bit system, but will my code run well on a 32 bit system if I store data in 64 bit? 我坐在64位系统上,但如果我将数据存储在64位,我的代码是否可以在32位系统上运行良好?

Thank you! 谢谢!

Don't you worry about that. 你不担心吗? The long value will be stored in 2 memory addresses. long值将存储在2个内存地址中。 Int64 / long will always be 64bit, and Int32 / int will always be 32bit. Int64 / long将始终为64位, Int32 / int将始终为32位。

There are a few implications (concerning memory space and performance), but the most noticeable may be that write/read operations won't be atomic on 32bit systems, but you shouldn't expect them to be atomic anyway, since the c# specification makes no such guarantee. 有一些含义(关于内存空间和性能),但最明显的可能是写/读操作在32位系统上不是原子的,但你不应该指望它们是原子的,因为c#规范使得没有这样的保证。

Either way, the point is: this is not something you should ever worry about - the CLR abstracts these things away. 无论哪种方式,重点是:这不是你应该担心的事情 - CLR将这些东西抽象出来。 Use whichever type suits you best. 使用最适合您的类型。

On a 32 bit system, operations on a 32 bit integer can be performed in a single machine register. 在32位系统上,可以在单个机器寄存器中执行32位整数的操作。 But operations on a 64 bit integer require two machine registers, and are quite a bit less efficient than 32 bit operations. 但是对64位整数的操作需要两个机器寄存器,并且比32位操作的效率要低得多。 And of course, being twice the size, long uses more memory than int . 当然,两倍大小, long使用的内存比int If you have arrays of these types then you will consume your cache twice as fast if you use long . 如果你有这些类型的数组,那么如果使用long你将使用两倍的缓存。 That can also have performance implications. 这也可能会影响性能。

So, if you can use int , you should prefer it to long . 所以,如果你可以使用int ,你应该把它喜欢long However, if the 32 bits of range afforded by int are not sufficient for the correctness of your program, you would need to use long . 但是,如果int提供的32位范围不足以保证程序的正确性,则需要使用long

Of course, even though operations on 64 bit integers, are less efficient when executed on a 32 bit machine, only profiling would tell you whether or not it actually matters. 当然,即使64位整数上的操作在32位机器上执行时效率较低,但只有剖析才会告诉您它是否真正重要。

Perhaps the bottom line is that programmers should not be wilfully profligate. 也许最重要的是,程序员不应该故意挥霍。 Why use 64 bits, if 32 bits suffice? 为什么使用64位,如果32位就足够了?

Yes. 是。 The data type is supported on both. 两者都支持数据类型。 But the 64 bit integer isn't native to 32 bit processors. 但64位整数不是32位处理器的原生。 If you use a 64 bit type, and you compile for 64 bit only (you can choose x64 or x86 (=32bit) as a targer), then the compiler might use specific 64 bit instructions, making your application run a bit faster. 如果使用64位类型,并且只编译64位(可以选择x64或x86(= 32位)作为目标),则编译器可能会使用特定的64位指令,从而使您的应用程序运行得更快一些。 Theoretically, that is, in practice you will probably not notice this. 从理论上讲,实际上你可能不会注意到这一点。

The tradeoff is that that application will not run on a 32 bit platform. 权衡是该应用程序不能在32位平台上运行。 The other way around will work. 反过来会有效。 So if you plan to target both platforms, you need to either compile to 32 bit only, or compile two times, once for each platform. 因此,如果您计划针对两个平台,则需要编译为仅32位,或者编译两次,每个平台一次。

For the availability of the long type it doesn't matter. 对于long类型的可用性而言无关紧要。 You can always use it. 你总是可以使用它。

You can try yourself like this: 你可以尝试这样:

Console.WriteLine(sizeof(long));

Then compile it as x86 instead of AnyCPU and run it. 然后将其编译为x86而不是AnyCPU并运行它。 You can run 32 bit programs on 64 bit Windows as well - see what the result is. 您也可以在64位Windows上运行32位程序 - 看看结果如何。

There are even larger number types such as BigInteger and they also work on 32 and 64 bit operating systems. 还有更大的数字类型,如BigInteger ,它们也适用于32位和64位操作系统。

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

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