Article From:https://www.cnblogs.com/scuwangjun/p/9124610.html

integer calculation

Today do LeetCode see a problem, flip integer, one line of code.if ((newResult - tail) / 10 != result)It means that if you go beyond the scope, you will have some losses. It is a little unclear. Just reviewed the original code, the reverse code and the complement code. Next, let’s look at how the computer is calculated from two questions.

1. why Integer.MAX_VALUE + 1 = Integer.MIN_VALUE

We can test it with java code:

System.out.println(Integer.MAX_VALUE + 1);
System.out.println(Integer.MIN_VALUE);

Print:-2147483648
-2147483648
That is -2^31

We know that the range of values with symbolic integers is[-2^31 ~ 2^31-1],In computer, it is stored in the form of complement. First, we will review what is complement, inversion and original code.

The original number of the positive integer is the same as the complement and the symbol bit is fixed to 0.The symbol bit of a negative integer is 1:Counter code = original code (except the first symbol bit)Complement = reverse code + 1Take -1 as an example:Original code: 1000000000000000000000000000001Counter code: 11111111111111111111111111111110Complement: 11111111111111111111111111111111The inside of the computer is the causeWith the complement code stored and calculated, the process of -1-1 is-1 complement 11111111111111111111111111111111 - 1Get the complement of -2: 111111111111111111111Eleven billion one hundred and eleven million one hundred and eleven thousand one hundred and tenThen -2's reverse code: 11111111111111111111111111111101Then the original code of -2: 10000000000000000000000000000010

With this foundation, let’s look at whyInteger.MAX_VALUE + 1 = Integer.MIN_VALUE:

Integer.MAX_VALUEThe binary is:01111111111111111111111111111111+1 obtained10000000000000000000000000000000This is a complement, according to the complement, you can get the counter code:11111111111111111111111111111111Note: it may be found that 10000000000000000000000000000000 - 1 should be01111111111111111111111111111111 ah, because the sign bits of negative integer remain unchanged, so the symbol bit is changed from 0 to 1..And then the original code is obtained.10000000000000000000000000000000Because 00000000000000000000000000000000 has been expressed as 0, and the computer specifies that 10000000000000000000000000000000 represents the minimum value of a negative integer, that is, Integer.MIN_VALUE.So Integer.MIN_VALUE'sComplement: 10000000000000000000000000000000Counter code: 111111111111111111111111111Eleven thousand one hundred and elevenOriginal code: 10000000000000000000000000000000And then look at Integer.MIN_VALUE + 1Complement: 10000000000000000000000000000001Counter code: 10000000000000000000000000000000Original code: 11111111111111111111111111111111

2. why Integer.MAX_VALUE * 10 = -10

Integer.MAX_VALUE * 10Inside the computer, there are 10 Integer.MAX_VALUE additivity (it seems to be...).The binary of Integer.MAX_VALUE is:01111111111111111111111111111111firstFirst look at Integer.MAX_VALUE * 2.01111111111111111111111111111111 +01111111111111111111111111111111 = =11111111111111111111111111111110 (complement)11111111111111111111111111111101 (counter code)One hundred trillion00000000000000010 (original code)So Integer.MAX_VALUE * 2 = -2You can use System.out.println (Integer.MAX_VALUE * 2).Verify it!Next, you can calculate Integer.MAX_VALUE * 10 step by step, that is, 10 Integer.MAX_VALUE additivity.The final result is as follows:11111111111111111111111111110110 (complement)11111111111111111111111111110101 (counter code)10000000000000000000000000001010 (original code)That is, -10.

Similar Posts:

Link of this Article: Integer calculation

Leave a Reply

Your email address will not be published. Required fields are marked *