```
int reverse(int x) {
int a = 1 << 31;
int neg = 0;
if (x < 0){
neg = 1;
x = -x;
}
double res = 0;
while (x != 0){
res = res*10 + x%10;
x /= 10;
}
if (res > a - 1 || res < -a){
return 0;
}
res = (int) res;
if (neg){
res = -res;
}
return res;
}
```