DrinkMoreBoilingWater's blog

By DrinkMoreBoilingWater, 5 years ago, In English

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; }
| Write comment?
»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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?

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    You can try the following,

    __int128 a, b;

    a = read(); b = read(); print(a + b);

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Hello, i got this error message expected unqualified-id before __int128 , how can I handle this error? thanks

»
4 years ago, # |
  Vote: I like it -8 Vote: I do not like it

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.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +21 Vote: I do not like it

    CMIIW, __int128 isn't handled by cin and cout 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).

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it +16 Vote: I do not like it
      istream& operator >> (istream& in, __int128& num) {
        // Construct the answer in num, the same as read(num) from the blog, but using 
        // istream::get, istream::peek
        return in;
      }
      
      ostream& operator << (ostream& out, __int128 num) {
        // print the answer by either:
        //   - char-by-char using ostream::put
        //   - converting num to string and doing out << res_string
        return out;
      }
      

      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.

      istream& operator >> (istream& in, Point& p) {
          in >> p.x >> p.y;
          return in;
      }
      
      
      ostream& operator << (ostream& out, const Point& p) {
          out << setprecision(10) << fixed;
          out << p.x << ' ' << p.y;
          return out;
      }
      
      • »
        »
        »
        »
        10 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Thanks bro

      • »
        »
        »
        »
        10 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        For a more specific version of overloaded i/o operators, you can use this:

        istream &operator>>(istream &is,lll &v) {
            string s;
            is>>s;
            v=0;
            for(auto &it:s) if(isdigit(it)) v=v*10+it-'0';
            if(s[0]=='-') v*=-1;
            return is;
        }
        
        ostream &operator<<(ostream &os,const lll &v) {
            if(v==0) return (os<<"0");
            lll num=v;
            if(v<0) os<<'-',num=-num;
            string s;
            for(;num>0;num/=10) s.pb((char)(num%10)+'0');
            reverse(all(s));
            return (os<<s);
        }
        
    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Ah, thanks!

»
6 days ago, # |
Rev. 2   Vote: I like it -26 Vote: I do not like it

you can use cpp_int can make more than 100!