C++ 在 64 位数据模型上指定 64 位无符号整数文字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13158501/
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
Specifying 64-bit unsigned integer literals on 64-bit data models
提问by Zach Saw
As there are various types of 64-bit data models (LLP64/IL32P64, LP64/I32LP64, ILP64, SILP64), what is the standard conforming way of specifying 64-bit unsigned integer literals?
由于有各种类型的 64 位数据模型(LLP64/IL32P64、LP64/I32LP64、ILP64、SILP64),指定 64 位无符号整数文字的标准符合方式是什么?
Would specifying the suffix of ULL be sufficient? Or would I end up causing the literal to be interpreted as 128-bit on some data models?
指定 ULL 的后缀就足够了吗?或者我最终会导致文字在某些数据模型上被解释为 128 位?
回答by unwind
You should use <cstdint> / <stdint.h>
, if you have it. This will give you:
你应该使用<cstdint> / <stdint.h>
,如果你有的话。这会给你:
uint64_t
is an unsigned integer type which is 64 bits in sizeUINT64_C()
is a macro for creating constants of the typeuint64_t
, by having the macro append the proper suffix.
uint64_t
是大小为 64 位的无符号整数类型UINT64_C()
是一个用于创建类型常量uint64_t
的宏,通过让宏附加正确的后缀。
回答by DevSolar
To my knowledge, there is no way to suffix an integer literal to a specific bit width; your only options are l, ul, ll, and ull.
据我所知,无法将整数文字作为特定位宽的后缀;您唯一的选择是 l、ul、ll 和 ull。
If you are paranoid about it, you would have to wrap your literals in an #if
checking for the sizes of long
/ long long
.
如果您对此感到偏执,则必须将文字包装在#if
检查long
/的大小中long long
。
Then again, as KennyTM pointed out above, as long as your literals arein the 64bit range andassigned to a 64bit value, it does not matter much if the literal itself is 64 or 128 bit, or does it?
话又说回来,作为KennyTM上文所指出的,只要你的文字是在64位范围内,并分配到64位的值,它并没有多大关系,如果字面本身是64或128位,或不是吗?
回答by Wes Hardaker
C and C++ don't have standardized 32/64/128 bit variable types. A long, for example, on some systems is 32 bits and 64 on others. It's annoying, but most OSes do provide some better typedefs to help you with, such as uint32
, etc, so you con select an exact type that you need.
C 和 C++ 没有标准化的 32/64/128 位变量类型。例如,在某些系统上,long 是 32 位,而在其他系统上是 64 位。这很烦人,但大多数操作系统确实提供了一些更好的 typedef 来帮助您,例如uint32
等,因此您可以选择所需的确切类型。
This is the job of a good configure
script: to determine what the system provides, test that it works, and help you select the right type for the right architecture you're running on.
这是一个好的configure
脚本的工作:确定系统提供什么,测试它是否工作,并帮助您为正在运行的正确架构选择正确的类型。
回答by Pete Becker
For the most part, it doesn't matter. If you don't give it a suffix, the type of an integer literal is determined by its value. If the compiler has a 32-bit unsigned long
and a 64-bit unsigned long long
, an unsigned value that is too large to fit in an unsigned long
but not too large for an unsigned long long
will have type unsigned long long
.
在大多数情况下,这无关紧要。如果你不给它一个后缀,整数文字的类型由它的值决定。如果编译器有一个 32 位unsigned long
和一个 64 位unsigned long long
,一个太大unsigned long
而不能放入但又不能太大的无符号值unsigned long long
将具有 type unsigned long long
。