classSolution { Public: intReverseintx) {Longans=0;//long is afraid of the middle process overflowintmin=1<< to, max=-min-1;//Judging overflow,<< for left shift operation while(x!=0) {ans=ans*Ten+x%Ten; X=x/Ten; } if(ans>max| | ans<min)return 0; returnans; }};
Comments:
This is the problem of the binary conversion.
Overflow must be considered during the conversion process.
Clear the range of representations for each type.
Report
32-bit platform:
unsigned int 0~4294967295
int-2147483648~2147483647 ( -2^31~ (2^31-1))//complement more than the original code to represent 1 numbers
unsigned long 0~4294967295
long-2147483648~2147483647
The maximum value of long long: 9223372036854775807
The minimum value of long long:-9223372036854775808
Unsigned the maximum value of long long: 1844674407370955161
Maximum value of __int64:9223372036854775807
Minimum value of __int64:9223372036854775808
Maximum value of unsigned __int64:18446744073709551615
Reverse digits of an integer.