int x = -2147483648;
boolean negative = false;
if(x<0){
negative=true;
x=(0-x);
}
long k = x;
long reverseNum = 0;
while(k!=0){
reverseNum *= 10;
reverseNum += k % 10;
k /= 10;
}
if( reverseNum>Integer.MAX_VALUE)
System.out.println(0);
else
System.out.println(negative ? 0-(int)reverseNum : (int)reverseNum);
It should return 0 but it is returning -126087180
It is working fine when I move "long k = x" to second line of code.
Can someone help me in understanding what is the actual issue with the code implementation and why I am getting a different result in the first case?