# How do we handle the overflow case?

• Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).

• As a cpp coder, I will use the `long long` to handle the overflow case. Since all input number is in `int` range, there is no way to overflow the `long long`.

• ``````                             int reverse(int x) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int r=0;
for (; x; x /= 10)
{
r = r * 10 + x % 10;
}
if((r>INT_MAX)||(r<INT_MIN))
return 0;
return r;
}};//if overflow, return 0;
``````

• Could you please format your code properly? Select your code and click on the `{}` button to format it. Also, it would be great to add a few sentences to explain your approach.

• how come if((r>INT_MAX)||(r<INT_MIN))is coming in play , if r ever cross integer limit it will automatically convert in limit, eg. if input is 2147483646 , so output should be 6463847412 (which mean error should return 0) but this code will result -2126087180.
correct me if i am wrong

• you are right, so if the reversed int is negative, I tried to return -1 and got accepted.

• even this code is getting accepted due to lack of good test cases. if your input is negative then your code will fails.

• function should return int, but not long long

• actually , you can assign a long long value to a int, if it can be reversed.

• I will just treat them as strings.

• Could you elaborate on that answer?

• I will let it return 0. Unfortunately, leetcode does not have the test case which covers the overflow. @1337, please consider to add test case "0x7fffffff". It should return 0. If there is no overflow check, it will return a garbage.
Here is my code:

``````class Solution {
public:
int reverse(int x) {
bool neg=false;
if (x < 0) neg = true, x = -x;
unsigned int ret = 0;
while (x / 10){
ret = ret*10+ x%10;
x = x / 10;
}
ret =ret*10+x;
if (ret>0x7fffffff) return 0; //overflow check.
if (neg) return -(int)ret;
return (int)ret;
}
};
``````

• if( result < result*10+temp )

``````    		result = result*10+temp;

else

exit(1);``````

• did you test your method and test any cases, I'm afraid a long long data will be truncated when transformed into a int data.

• Nope, I just use it in the transforming process. When returning result, I will compare it with Max_int to handle the overflow case.

• How about use INT_MAX to replace 0x7fffffff? Maximum value could vary from computer to computer, INT_MAX specifies the max value that can be stored in local computer, it's defined in "limits.h".

• Using unsigned int is unsafe and may also be overflow after reversing ret. long long is safe.

• Yeah I did it just by using stringstream

• you mean you will redesign the function to make it return a string? sounds good.

• I think returning a 0 on overflow works.

You can refer to below code.
It's using a larger data type (long ) to check smaller data type's overflow.

``````public static int checkedAdd(int a, int b) {
long result = (long) a + b;
checkNoOverflow(result == (int) result);
return (int) result;
}``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.