128-bit integers in C++

I tried doing this problem yesterday.
I wanted to see if 128-bit integers work in C++ (please ignore “You may assume that the final value of C will fit in a 32-bit Pascal LongInt or a C long” in the statement).
The maximum value which can be computed is ^{100}C_{50} \approx 1.01 \times 10^{29} (which should fit in a 128-bit integer).
I tried using the __int128 data type in C++ but it doesn’t work.
I wrote a sample program just to test the output.

#include <bits/stdc++.h>

using namespace std;

int main()
{
    __int128 n = 1;
    cout << n << '\n';
}

I got the following error:

main.cpp: In function ‘int main()’:
main.cpp:8:10: error: ambiguous overload for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘__int128’)
     cout << n << '\n';
     ~~~~~^~~~
In file included from /usr/include/c++/6/istream:39:0,
                 from /usr/include/c++/6/sstream:38,
                 from /usr/include/c++/6/complex:45,
                 from /usr/include/c++/6/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/6/bits/stdc++.h:52,
                 from main.cpp:1:
/usr/include/c++/6/ostream:166:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char; _Traits = std::char_traits; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream]
       operator<<(long __n)
       ^~~~~~~~
/usr/include/c++/6/ostream:170:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char; _Traits = std::char_traits; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream]
       operator<<(unsigned long __n)
       ^~~~~~~~
/usr/include/c++/6/ostream:174:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char; _Traits = std::char_traits; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream]
       operator<<(bool __n)
       ^~~~~~~~
In file included from /usr/include/c++/6/ostream:638:0,
                 from /usr/include/c++/6/istream:39,
                 from /usr/include/c++/6/sstream:38,
                 from /usr/include/c++/6/complex:45,
                 from /usr/include/c++/6/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/6/bits/stdc++.h:52,
                 from main.cpp:1:
/usr/include/c++/6/bits/ostream.tcc:91:5: note: candidate: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char; _Traits = std::char_traits]
     basic_ostream<_CharT, _Traits>::
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/6/istream:39:0,
                 from /usr/include/c++/6/sstream:38,
                 from /usr/include/c++/6/complex:45,
                 from /usr/include/c++/6/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/6/bits/stdc++.h:52,
                 from main.cpp:1:
/usr/include/c++/6/ostream:181:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char; _Traits = std::char_traits; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream]
       operator<<(unsigned short __n)
       ^~~~~~~~
In file included from /usr/include/c++/6/ostream:638:0,
                 from /usr/include/c++/6/istream:39,
                 from /usr/include/c++/6/sstream:38,
                 from /usr/include/c++/6/complex:45,
                 from /usr/include/c++/6/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/6/bits/stdc++.h:52,
                 from main.cpp:1:
/usr/include/c++/6/bits/ostream.tcc:105:5: note: candidate: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char; _Traits = std::char_traits]
     basic_ostream<_CharT, _Traits>::
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/6/istream:39:0,
                 from /usr/include/c++/6/sstream:38,
                 from /usr/include/c++/6/complex:45,
                 from /usr/include/c++/6/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/6/bits/stdc++.h:52,
                 from main.cpp:1:
/usr/include/c++/6/ostream:192:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char; _Traits = std::char_traits; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream]
       operator<<(unsigned int __n)
       ^~~~~~~~
/usr/include/c++/6/ostream:201:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char; _Traits = std::char_traits; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream]
       operator<<(long long __n)
       ^~~~~~~~
/usr/include/c++/6/ostream:205:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char; _Traits = std::char_traits; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream]
       operator<<(unsigned long long __n)
       ^~~~~~~~
/usr/include/c++/6/ostream:220:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char; _Traits = std::char_traits; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream]
       operator<<(double __f)
       ^~~~~~~~
/usr/include/c++/6/ostream:224:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char; _Traits = std::char_traits; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream]
       operator<<(float __f)
       ^~~~~~~~
/usr/include/c++/6/ostream:232:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char; _Traits = std::char_traits; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream]
       operator<<(long double __f)
       ^~~~~~~~
/usr/include/c++/6/ostream:502:5: note: candidate: std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char) [with _CharT = char; _Traits = std::char_traits]
     operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
     ^~~~~~~~
/usr/include/c++/6/ostream:508:5: note: candidate: std::basic_ostream& std::operator<<(std::basic_ostream&, char) [with _Traits = std::char_traits]
     operator<<(basic_ostream<char, _Traits>& __out, char __c)
     ^~~~~~~~
/usr/include/c++/6/ostream:514:5: note: candidate: std::basic_ostream& std::operator<<(std::basic_ostream&, signed char) [with _Traits = std::char_traits]
     operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
     ^~~~~~~~
/usr/include/c++/6/ostream:519:5: note: candidate: std::basic_ostream& std::operator<<(std::basic_ostream&, unsigned char) [with _Traits = std::char_traits]
     operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
     ^~~~~~~~

I can’t print 128-bit integers. I tried printing to_string(n) which also didn’t work. How to fix this issue? Also can I perform arithmetic operations on such integers?

EDIT: Libraries such as Boost aren’t supported in online judges. I request you to suggest something else.

1 Like

Boost isn’t supported in online judges though. Any alternative?

It is only supported on CC. I would recommend you to use string for big numbers

1 Like

Can you please elaborate on how I can perform arithmetic operations on strings?

Search on google any arithmetic operation with string. You will find

I did find stuff. But is there anything that I can do for the 128-bit integer issue? This approach is much more simple imo.

String arithmetic is applicable at every place.

You can overload the << operator to handle such large number. Adding the function to your code will solve your issue.

C++ Code to handle 128 bit integers

Credits: Stack overflow link