C语言 在 Linux 中定义 64 位宽度的整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2347884/
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
Define the 64 bit width integer in Linux
提问by Yongwei Xing
I try to define a 64 bits width integer using C language on Ubnutu 9.10. 9223372036854775808 is 2^23
我尝试在 Ubnutu 9.10 上使用 C 语言定义一个 64 位宽度的整数。9223372036854775808 是 2^23
long long max=9223372036854775808
long max=9223372036854775808
When I compile it, the compiler gave the warning message:
当我编译它时,编译器给出了警告信息:
binary.c:79:19: warning: integer constant is so large that it is unsigned
binary.c: In function ‘bitReversal':
binary.c:79: warning: this decimal constant is unsigned only in ISO C90
binary.c:79: warning: integer constant is too large for ‘long' type
Is the long type 64 bits width?
长型是64位宽吗?
Best Regards,
此致,
回答by kennytm
long long max=9223372036854775808LL; // Note the LL
// long max=9223372036854775808L; // Note the L
A long longtype is at least 64 bit, a longtype is at least 32 bit. Actual width depends on the compiler and targeting platform.
一个long long类型至少是 64 位,一个long类型至少是 32 位。实际宽度取决于编译器和目标平台。
Use int64_tand int32_tto ensure the 64-/32-bit integer can fit into the variable.
使用int64_t和int32_t确保 64 位/32 位整数可以放入变量中。
回答by caf
The problem you are having is that the number you're using (9223372036854775808) is 2**63, and the maximum value that your long longcan hold (as a 64 bit signed 2s complement type) is one less than that - 2**63 - 1, or 9223372036854775807 (that's 63 binary 1s in a row).
您遇到的问题是您使用的数字 (9223372036854775808) 是2**63,并且您long long可以保存的最大值(作为 64 位有符号的 2s 补码类型)比那个少一个 - 2**63 - 1,或 9223372036854775807(即 63 二进制 1连续)。
Constants that are too large for long long(as in this case) are given type unsigned long long, which is why you get the warning integer constant is so large that it is unsigned.
对于long long(在这种情况下)来说太大的常量被赋予 type unsigned long long,这就是您收到警告的原因integer constant is so large that it is unsigned。
The longtype is at least 32 bits, and the long longtype at least 64 bits (both including the sign bit).
的long类型是至少32位,和long long类型的至少64位(两者包括符号位)。
回答by Nicolas Guillaume
I'm not sure it's will fix your problem (The LL solution looks good). But here is a recomandation :
我不确定它会解决您的问题(LL 解决方案看起来不错)。但这里有一个建议:
You should use hexadecimal notation to write the maximal value of something.
你应该使用十六进制表示法来写一些东西的最大值。
unsigned char max = 0xFF;
unsigned short max = 0xFFFF;
uint32_t max = 0xFFFFFFFF;
uint64_t max = 0xFFFFFFFFFFFFFFFF;
As you can see it's readable.
如您所见,它是可读的。
To display the value :
要显示值:
printf("%lld\n", value_of_64b_int);
printf("%lld\n", value_of_64b_int);
回答by Nicolas Guillaume
I think it depends on what platform you use. If you need to know exactly what kind of integer type you use you should use types from Stdint.hif you have this include file on you system.
我认为这取决于您使用的平台。如果您需要确切地知道您使用的是哪种整数类型,您应该使用Stdint.h 中的类型,如果您的系统上有这个包含文件。

