As an extension the integer scalar type __int128 is supported for targets which have an integer mode wide enough to hold 128 bits. Simply write __int128 for a signed 128-bit integer, or unsigned __int128 for an unsigned 128-bit integer. There is no support in GCC for expressing an integer constant of type __int128 for targets with long long integer less than 128 bits wide.
Here are some tips about __int128 read, print, compare
__int128 read() {
__int128 x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
void print(__int128 x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) print(x / 10);
putchar(x % 10 + '0');
}
bool cmp(__int128 x, __int128 y) { return x > y; }
Hello. Thank you for your article. How can I use read() to read the numbers? After declaring your functions I wrote in main __int128 a, b; read(a); read(b); print(a + b); and it didnt work. No idea how to used read(). Can you help?
You can try the following,
__int128 a, b;
a = read(); b = read(); print(a + b);
Hello, i got this error message
expected unqualified-id before __int128
, how can I handle this error? thanksIf you are using Windows, make sure your Windows support C++ 64-bit.
You can refer to this blog https://mirror.codeforces.com/blog/entry/75004
Thanks for sharing this useful information. The following is a slight update to your read and write function that deals with larger values of unsigned 128-bit integers.
G++ support for 128-bit integers
Thanks Dude... That was exactly what i needed!!
With pleasure.
Check the previous link for a slight update to the code.
Are these read/print/compare functions necessary, or can we just use cin >>, cout <<, and >?
By the way, I think this comparator looks like it's backwards compared to a typical C++ comparator, so watch out for that.
CMIIW, __int128 isn't handled by
cin
andcout
by default. But you can make your own<<
operator for I/O streams (although I don't have any resources regarding how to do that).You can also overload there operators for Point class in geometric problems, making it easier to debug them. Example for Point with floating-point coordinates.
Thanks bro
For a more specific version of overloaded i/o operators, you can use this:
Ah, thanks!
you can use cpp_int can make more than 100!