After solving some C++ problems from LeetCode I came across to the Reverse Integer and realized I can not really grasp how to deal with integer overflow.
The problem is simple and consists in reversing an integer number (e.g. 123 -> 321). Now, the difficult part is complying with the following limitation:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows
What I did was the following:
int reverse(int x) { int temp = 0; while(x != 0){ // pop int pop = x%10; x /= 10; if(temp < (INT_MIN - pop)/10 || temp > (INT_MAX - pop)/10){ return 0; } else{ // push temp = temp*10 + pop; } } return temp; } Unfortunately, the code does not prevent the overflow. What am I doing wrong?

(INT_MAX + pop)overflowsINT_MAX, it's never going to be becauseINT_MAXis, by definition, the biggest.INT_MIN - popunderflows.