C/C++中的64位整数(__int64 and long long)
来源:互联网 发布:js上拉加载原理 编辑:程序博客网 时间:2024/05/22 01:29
在做ACM题时,经常都会遇到一些比较大的整数。而常用的内置整数类型常常显得太小了:其中long 和 int 范围是[-2^31,2^31),即-2147483648~2147483647。而unsigned范围是[0,2^32),即0~4294967295。也就是说,常规的32位整数只能够处理40亿以下的数。
那遇到比40亿要大的数怎么办呢?这时就要用到C++的64位扩展了。不同的编译器对64位整数的扩展有所不同。基于ACM的需要,下面仅介绍VC6.0与g++编译器的扩展。
VC的64位整数分别叫做__int64与unsigned __int64,其范围分别是[-2^63, 2^63)与[0,2^64),即-9223372036854775808~9223372036854775807与0~18446744073709551615(约1800亿亿)。对64位整数的运算与32位整数基本相同,都支持四则运算与位运算等。当进行64位与32位的混合运算时,32位整数会被隐式转换成64位整数。但是,VC的输入输出与__int64的兼容就不是很好了,如果你写下这样一段代码:
1 __int64 a;
2 cin >> a;
3 cout << a;
那么,在第2行会收到“error C2679: binary '>>' : no operator defined which takes a right-hand operand of type '__int64' (or there is no acceptable conversion)”的错误;在第3行会收到“error C2593: 'operator <<' is ambiguous”的错误。cout,cin对于__int64无能为力,写法较复杂,不过也经常不用。那是不是就不能进行输入输出呢?当然不是,你可以使用C的写法:
scanf("%I64d",&a);
printf("%I64d",a);
就可以正确输入输出了。当使用unsigned __int64时,把"I64d"改为"I64u"就可以了。
OJ通常使用g++编译器。其64位扩展方式与VC有所不同,它们分别叫做long long 与 unsigned long long。处理规模与除输入输出外的使用方法同上。对于输入输出,它的扩展比VC好。既可以使用
1 long long a;
2 cin>>a;
3 cout<<a;
也可以使用
scanf("%lld",&a);
printf("%lld",a);
使用无符号数时,将"%lld"改成"%llu"即可。
http://hi.baidu.com/fandywang_jlu/blog/item/7c8e98cc0db2081601e928a1.html
- C/C++中的64位整数(__int64 and long long)
- C/C++中的64位整数(__int64 and long long)
- C/C++中的64位整数(__int64 and long long)
- C/C++中的64位整数(__int64 and long long)
- C/C++中的64位整数(__int64 and long long)
- __int64 与long long 的区别, C/C++的64位整型 不同编译器间的比较
- C/C++中long long与__int64的区别
- C/C++中long long与__int64的区别
- C/C++中关于__int64与long long 的区别
- 【转载】C/C++中long long与__int64的区别
- C语言中的整数(short,int,long)
- 30 C语言中的整数(short,int,long)
- C++中的long long和__int64类型(转载)
- C语言中long long的用法 C++ __int64用法 __int64的printf问题
- C++中的long long和__int64类型
- C++中的long long和__int64类型
- C++中的long long和__int64类型
- C++中的long long和__int64类型
- asp.net先于iis安装导致iis不能解析aspx的问题(Failed to access IIS metabase,IISメタベースにアクセスできませんでした)
- Modultils工具源码分析之insmod篇 (11)
- 存储过程入门
- 使用ant的unzip需要注意编码
- matlab java.lang.OutOfMemoryError: Java heap space
- C/C++中的64位整数(__int64 and long long)
- Linux 下 Mysql配置问题集
- c++的一些零碎
- 近来游览各类网站有感
- 初始化窗口
- 在ubuntu 8.10下使用kermit
- Color命名空间
- DBOperation的缺省实现
- Static 的简介