How to print __int128 in g++?

前端 未结 6 1763
再見小時候
再見小時候 2020-12-05 06:48

I am using the GCC built-in type __int128 for a few things in my C++ program, nothing really significant, at least not enough to justify to use BigInt library o

6条回答
  •  情话喂你
    2020-12-05 07:11

    A deceptively simple approach

    std::ostream& operator<<(std::ostream& os, __int128 x){
        if(x<0) return os << "-" << -x;
        if(x<10) return  os << (char)(x+'0');
        return os << x/10 << (char)(x%10+'0');
    }
    

    See my other comment for a failed attempt at a more performant implementation.

提交回复
热议问题