Java numerical overflow problem and simple algorithm example

1. What is data overflow

Data overflow is:
when a certain type of value has reached the maximum value that this type can reach, then continue to expand, or reach the minimum value and then continue to shrink, there will be a data overflow problem.
Basic types in Java have a range, beyond which the value will overflow.
Take Int as an example. The
int type is "signed" in Java. The so-called "signed" means positive and negative.
The first digit represents the positive or negative of our value.
If the first digit is 0, it is positive, and 1 is negative. The difference between positive and negative is therefore the inversion plus one.
The int in Java is 32 bits in total. In the case of a positive upper limit, the first digit can only be 0, and the other digits can be 1 (in the case of 2^31-1). But if the positive number is too large, such as 2^31, the computer has to change the first digit to 1, and soon forgets that this is an overflow situation, and outputs it in the normal way, so it becomes negative. In fact, it can't be blamed. It can't automatically deal with overflow situations, because 32-bit is fixed, and it cannot be temporarily expanded to 33-bit due to overflow.
2^31-1 = 0111 1111 1111 1111 1111 1111 1111 1111 = 2147483647
2^31 = 2^31-1 + 1 = 1000 0000 0000 0000 0000 0000 0000 0000 = -2147483648 and

above are negative numbers. The same goes for overflow to 0. You think if a number is so large that the last 32 bits are all 0, the computer can only recognize it as 0. There are many such cases. For example, 2^32 is a total of 33 bits, the first bit is 1, and the following 32 bits are all 0.
Some blogs introduce this more detailed
value overflow. Detailed blog address

2. General application

Let's take a simple algorithm problem of Leetcode to illustrate that
given a 32-bit signed integer, you need to reverse the digits on each of the integers.

示例 1:

输入: 123
输出: 321
 示例 2:

输入: -123
输出: -321
示例 3:

输入: 120
输出: 21
注意:

假设我们的环境只能存储得下 32 位的有符号整数,
则其数值范围为 [−231,  231 − 1]。请根据这个假设,
如果反转后整数溢出那么就返回 0。

First look at the wrong solution. The first reason for the error is that the occurrence of negative numbers is not considered, and the other is that the value may overflow

class Solution {
    
    
    public int reverse(int x) {
    
    
        String s ="";
        s= x%10+"";//3
        s+=x/10%10;//2
        s =s+ x/100;//1
        int c =Integer.parseInt(s);
        return c;
    }
}

Generally, we are the ones digit + tens digit 10 + hundred digit 100,
then it is possible that when the tens digit * 10 + the single digit, it exceeds our data range, this is a memory overflow, and
finally get a correct solution.

  public int reverse(int x) {
    
    
  	int ans =0;
  	while(x!=0){
    
    
		if((ans*10)/10!=ans)
		{
    
    
			ans=0;//说明我们的ans数值溢出了	这里ans*10不溢出那么ans*10+x%10(个位数)				
				//会溢出么  不会  因为我们的个位是0~9  不会进位 所以不会溢出
			break;
		}
		ans = ans*10+x%10;
		x/=10;
	}
	return ans;
  }

If there is an error or disagreement, please point it out

Guess you like

Origin blog.csdn.net/qq_22155255/article/details/109784015