guest@cp-base:~/home/utilities$
templates/int128-io.cpp
compilable
$cat templates/int128-io

128-bit Integer I/O

Read and print __int128 values — a GCC extension for 128-bit signed integers not supported by standard cin/cout.

[Utilities]|
Jul 9, 2026
|explanatory_notes.md|
#int128#io#gcc-extension#large-numbers
Log in to track progress, save custom versions, and organize into collections.Log In
$cat source_code/
cpp
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <bits/stdc++.h>
using namespace std;

__int128 read128() {
    __int128 x = 0;
    int f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = x * 10 + (ch - '0');
        ch = getchar();
    }
    return x * f;
}

void print128(__int128 x) {
    if (x < 0) { putchar('-'); x = -x; }
    if (x > 9) print128(x / 10);
    putchar(x % 10 + '0');
}
23 linesutf-8
$cat explanation_notes.md
notes_viewer --renderedmarkdown (math enabled)

128-bit Integer I/O

GCC provides __int128 for 128-bit signed integers (range ±1.7×1038\approx \pm 1.7 \times 10^{38}), but cin/cout/scanf/printf don't support it. These functions handle I/O manually.

How It Works

  • read() — parses characters via getchar(), handles negative sign, accumulates digits into __int128

  • print() — handles negatives, recursively divides by 10 to output digits left-to-right via putchar()
  • When to Use

  • When intermediate products exceed long long range (e.g., 1018×101810^{18} \times 10^{18})

  • Geometry cross products with large coordinates

  • Modular arithmetic where mod is close to 101810^{18}
  • Complexity

  • Time — O(d)O(d) per read/print where dd is the number of digits

  • Space — O(d)O(d) stack for recursive print
  • Notes

  • GCC/Clang only — not portable to MSVC

  • Most CP judges (Codeforces, AtCoder) support it

  • Alternative: use __int128 only for intermediate calculations and convert to long long for I/O