From 10060efd25d631e742ed0ab0bc460f680b00ca3b Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Wed, 22 Feb 2023 16:26:40 +0800 Subject: [PATCH] fix: int128 ostream helper --- utils/int128.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/utils/int128.h b/utils/int128.h index ee690524..dcb46b06 100644 --- a/utils/int128.h +++ b/utils/int128.h @@ -2,12 +2,33 @@ #include #include +/** + * Prints an 128-bit integer to an output stream. + */ std::ostream& operator<<(std::ostream& __ostream, __int128 __n) { std::string __o; + + if (__n == 0) { + return __ostream << 0; + } + + bool is_negative = false; + + if (__n < 0) { + is_negative = true; + __n = -__n; + } + while (__n) { __o.push_back(__n % 10 + '0'); __n /= 10; } + std::reverse(__o.begin(), __o.end()); + + if (is_negative) { + __ostream << '-'; + } + return __ostream << __o; }