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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:02:47  来源:igfitidea点击:

Specifying 64-bit unsigned integer literals on 64-bit data models

c++integer64-bit

提问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_tis an unsigned integer type which is 64 bits in size
  • UINT64_C()is a macro for creating constants of the type uint64_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 #ifchecking 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 configurescript: 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 longand a 64-bit unsigned long long, an unsigned value that is too large to fit in an unsigned longbut not too large for an unsigned long longwill have type unsigned long long.

在大多数情况下,这无关紧要。如果你不给它一个后缀,整数文字的类型由它的值决定。如果编译器有一个 32 位unsigned long和一个 64 位unsigned long long,一个太大unsigned long而不能放入但又不能太大的无符号值unsigned long long将具有 type unsigned long long