I'm trying to use 64 bit integers in C, but am getting mixed signals as to whether it should be possible.
我尝试在C中使用64位整数,但是对于是否应该这样,我得到了混合的信号。
When I execute the printf:
当我执行printf:
printf("Size of long int:%d\nSize of long long int:%d\n\n",(int)sizeof(long int), (int)sizeof(long long int));
The response I get is:
我得到的回答是:
Size of long int:4 Size of long long int:8
长int数:4长整数:8。
This makes me feel that a long long int has 8 bytes = 64 bits.
这让我觉得长时间的int有8个字节= 64位。
However, when I try to declare the following variables:
但是,当我尝试声明以下变量时:
long long int a2 = 0x00004444;
long long int b2 = 0x000044440;
long long int c2 = 0x0000444400;
long long int d2 = 0x00004444000;
long long int e2 = 0x000044440000;
long long int f2 = 0x0000444400004;
long long int g2 = 0x00004444000044;
long long int h2 = 0x000044440000444;
long long int i2 = 0x0000444400004444;
The last 4 variables (f2,g2,h2,i2) give me the error message:
最后4个变量(f2,g2,h2,i2)给出了错误信息:
warning: integer constant is too large for ‘long’ type
警告:整数常量对于“long”类型来说太大了。
I get the same result when I replace 'long long int' with 'int64_t'. I assume 'int64_t' was recognized, since it didn't generate any error messages of its own.
当我用“int64_t”替换“long long int”时,我得到了相同的结果。我假设“int64_t”是被识别的,因为它自己没有生成任何错误消息。
So, it appears my 8 byte long long int is really a 6 byte long long int, and I don't understand what I'm missing here. If it's any help, here is the information on my gcc compiler:
所以,我的8字节长int是一个6字节长的整数,我不明白我在这里缺少什么。如果有任何帮助,这里是我的gcc编译器的信息:
me@ubuntu:~$ gcc -v
Using built-in specs.
Target: i686-linux-gnu
Configured with: ../src/configure -v
--with-pkgversion='Ubuntu/Linaro 4.4.4-14ubuntu5'
--with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++
--prefix=/usr
--program-suffix=-4.4
--enable-shared
--enable-multiarch
--enable-linker-build-id
--with-system-zlib
--libexecdir=/usr/lib
--without-included-gettext
--enable-threads=posix
--with-gxx-include-dir=/usr/include/c++/4.4
--libdir=/usr/lib
--enable-nls
--with-sysroot=/ -
-enable-clocale=gnu
--enable-libstdcxx-debug
--enable-objc-gc
--enable-targets=all
--disable-werror
--with-arch-32=i686
--with-tune=generic
--enable-checking=release
--build=i686-linux-gnu
--host=i686-linux-gnu
--target=i686-linux-gnu
Thread model: posix
gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5)
If anyone knows how (or if) 64 bit integers are accessible to me, I'd really appreciate any help. Thanks....
如果有人知道(或者如果)64位整数对我来说是可以访问的,我将非常感谢任何帮助。由于....
5 个解决方案
#1
58
Use stdint.h
for specific sizes of integer data types, and also use appropriate suffixes for integer literal constants, e.g.:
使用stdint。h对于整数数据类型的特定大小,并对整型文字常量使用适当的后缀,例如:
#include <stdint.h>
int64_t i2 = 0x0000444400004444LL;
#2
27
Try an LL
suffix on the number, the compiler may be casting it to an intermediate type as part of the parse. See http://gcc.gnu.org/onlinedocs/gcc/Long-Long.html
在这个数字上尝试一个LL后缀,编译器可以将它转换为一个中间类型,作为解析的一部分。参见http://gcc.gnu.org/onlinedocs/gcc/Long-Long.html
long long int i2 = 0x0000444400004444LL;
long long int i2 = 0x00004444000044ll;
Additionally, the the compiler is discarding the leading zeros, so 0x000044440000
is becoming 0x44440000
, which is a perfectly acceptable 32-bit integer (which is why you aren't seeing any warnings prior to f2
).
另外,编译器丢弃了前导零,所以0x000044440000变成了0x44440000,这是一个完全可以接受的32位整数(这就是为什么在f2之前没有看到任何警告)。
#3
4
Use int64_t
, that portable C99 code.
使用int64_t,这个可移植的C99代码。
int64_t var = 0x0000444400004444LL;
For printing:
印刷:
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
printf("blabla %" PRIi64 " blabla\n", var);
#4
3
How to specify 64 bit integers in c
如何在c中指定64位整数?
Going against the usual good idea to appending LL
.
违背通常的好想法去附加LL。
Appending LL
to a integer constant will insure the type is at least as wide as long long
. If the integer constant is octal or hex, the constant will become unsigned long long
if needed.
将LL添加到一个整数常量将确保该类型的宽度至少为长。如果整型常数是八进制或十六进制,如果需要,常数将会在长时间内无符号。
If ones does not care to specify too wide a type, then LL
is OK. else, read on.
如果用户不关心指定太宽的类型,那么就可以了。其他,请继续阅读。
long long
may be wider than 64-bit.
long long可能比64位更宽。
Today, it is rare that long long
is not 64-bit, yet C specifies long long
to be at least 64-bit. So by using LL
, in the future, code may be specifying, say, a 128-bit number.
今天,很少见的是,long long并不是64位的,但是C指定了很长时间至少是64位的。因此,通过使用LL,在将来,代码可以指定,比如说,一个128位的数字。
C has Macros for integer constants which in the below case will be type int_least64_t
C有整数常量,在下面的情况下将是类型int_最少64_t。
#include <stdint.h>
#include <inttypes.h>
int main(void) {
int64_t big = INT64_C(9223372036854775807);
printf("%" PRId64 "\n", big);
uint64_t jenny = INT64_C(0x08675309) << 32; // shift was done on at least 64-bit type
printf("0x%" PRIX64 "\n", jenny);
}
output
输出
9223372036854775807
0x867530900000000
#5
1
Append ll suffix to hex digits for 64-bit (long long int), or ull suffix for unsigned 64-bit (unsigned long long)
对64位(long long int)的十六进制数字加后缀ll后缀,或ull后缀用于未签名的64位(无符号long long)
#1
58
Use stdint.h
for specific sizes of integer data types, and also use appropriate suffixes for integer literal constants, e.g.:
使用stdint。h对于整数数据类型的特定大小,并对整型文字常量使用适当的后缀,例如:
#include <stdint.h>
int64_t i2 = 0x0000444400004444LL;
#2
27
Try an LL
suffix on the number, the compiler may be casting it to an intermediate type as part of the parse. See http://gcc.gnu.org/onlinedocs/gcc/Long-Long.html
在这个数字上尝试一个LL后缀,编译器可以将它转换为一个中间类型,作为解析的一部分。参见http://gcc.gnu.org/onlinedocs/gcc/Long-Long.html
long long int i2 = 0x0000444400004444LL;
long long int i2 = 0x00004444000044ll;
Additionally, the the compiler is discarding the leading zeros, so 0x000044440000
is becoming 0x44440000
, which is a perfectly acceptable 32-bit integer (which is why you aren't seeing any warnings prior to f2
).
另外,编译器丢弃了前导零,所以0x000044440000变成了0x44440000,这是一个完全可以接受的32位整数(这就是为什么在f2之前没有看到任何警告)。
#3
4
Use int64_t
, that portable C99 code.
使用int64_t,这个可移植的C99代码。
int64_t var = 0x0000444400004444LL;
For printing:
印刷:
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
printf("blabla %" PRIi64 " blabla\n", var);
#4
3
How to specify 64 bit integers in c
如何在c中指定64位整数?
Going against the usual good idea to appending LL
.
违背通常的好想法去附加LL。
Appending LL
to a integer constant will insure the type is at least as wide as long long
. If the integer constant is octal or hex, the constant will become unsigned long long
if needed.
将LL添加到一个整数常量将确保该类型的宽度至少为长。如果整型常数是八进制或十六进制,如果需要,常数将会在长时间内无符号。
If ones does not care to specify too wide a type, then LL
is OK. else, read on.
如果用户不关心指定太宽的类型,那么就可以了。其他,请继续阅读。
long long
may be wider than 64-bit.
long long可能比64位更宽。
Today, it is rare that long long
is not 64-bit, yet C specifies long long
to be at least 64-bit. So by using LL
, in the future, code may be specifying, say, a 128-bit number.
今天,很少见的是,long long并不是64位的,但是C指定了很长时间至少是64位的。因此,通过使用LL,在将来,代码可以指定,比如说,一个128位的数字。
C has Macros for integer constants which in the below case will be type int_least64_t
C有整数常量,在下面的情况下将是类型int_最少64_t。
#include <stdint.h>
#include <inttypes.h>
int main(void) {
int64_t big = INT64_C(9223372036854775807);
printf("%" PRId64 "\n", big);
uint64_t jenny = INT64_C(0x08675309) << 32; // shift was done on at least 64-bit type
printf("0x%" PRIX64 "\n", jenny);
}
output
输出
9223372036854775807
0x867530900000000
#5
1
Append ll suffix to hex digits for 64-bit (long long int), or ull suffix for unsigned 64-bit (unsigned long long)
对64位(long long int)的十六进制数字加后缀ll后缀,或ull后缀用于未签名的64位(无符号long long)