GCC提供了两种128位整数类型,分别是__int128_t和__uint128_t,分别用于声明有符号整数变量和无符号整数变量。
有关GCC的文档参见:Using the GNU Compiler Collection (GCC)。
这里给出了样例程序,是有关类型__int128_t和__uint128_t的。从计算可以看出,这两个类型都是16字节的,类型__uint128_t是128位的。程序中使用了按位取反运算,移位运算和乘法运算。
由于这种大整数无法使用函数printf()输出其值,所以自己做了一个整数转字符串函数myitoa(),用于实现128位整数的输出。
编程操作系统是Ubuntu15.10,使用Qt编写程序,编译器是gcc的版本是5.2.1。
样例程序:
#include <iostream>
using namespace std;
void myitoa(__int128_t v, char* s)
{
char temp;
int i=0, j;
while(v >0) {
s[i++] = v % 10 + '0';
v /= 10;
}
s[i] = '\0';
j=0;
i--;
while(j < i) {
temp = s[j];
s[j] = s[i];
s[i] = temp;
j++;
i--;
}
}
int main()
{
__uint128_t n = 0;
n = ~n;
int count = 0;
while(n > 0) {
count++;
n >>= 1;
}
cout << "count=" << count << endl;
cout << "__uint128_t size=" << sizeof(__uint128_t) << endl;
cout << endl;
cout << "__int128_t size=" << sizeof(__int128_t) << endl;
__int128_t x = 1100000000000000L;
__int128_t y = 2200000000000000L;
char s[40];
x *= y;
myitoa(x, s);
cout << "x=" << s << endl;
return 0;
}
程序运行结果:
count=128
__uint128_t size=16
__int128_t size=16
x=2420000000000000000000000000000
程序运行结果截图: