@EvanPerlinHu, if its long long, then we check for overflow possibility before each operation so as not to invoke Undefined Behavior. (Though there is a faster way if we are going to rely on Undefined Behavior: recall that signed integer overflow in C++ is Undefined Behavior).

const auto Max = std::numeric_limits<long long>::max();
long long Ans = 0;
while(x != 0){
auto mod = x % 10;
if(Max / 10 < Ans) //Will it overflow if we do Ans *=10 ?
return 0;
Ans *= 10;
if(Max - mod < Ans) //Will it overflow if weadd mod to Ans ?
return 0;
Ans += mod;
x /= 10;
}

Of cause, we assume x is a positive integer, though we could always take out the sign before doing this operation and bring it back later