I think this is a very interesting question, what should the return value of Math.Abs (-2147483648) be? Why didn't the Java calculation result get a positive result? First, let's look at the principle that Java distinguishes positive and negative integers. In the case of binary, Java uses 0 and one to represent positive and negative, the highest bit-the first digit on the left is 1 for a negative number, and the highest bit is 0 to represent a positive number. In the 32-bit int binary representation, the highest bit is reserved for the sign. We know that the value of the Java 32-bit int is ( -2147483648,2147483647], converted to binary as follows: The maximum is: 2147483647, Binary: 01111111111111111111111111111111 The minimum value is:-2147483648, Binary: 10000000000000000000000000000000 and the binary positive and negative conversion is the inverse code + 1, the inverse code is the positive code of all the digits of the 0 and 1 swap. -2147483648 Binary Positive code: 10000000000000000000000000000000-2147483648 Binary Counter code: 01111111111111111111111111111111-2147483648 binary Inverse code +1:10000000000000000000000000000000 This binary number is the same as the original, so Math.Abs (-2147483648) equals-2147483648. Summary if the calculated number is larger, more than 1 billion of the kind, be sure to use long length shaping. Like the following. Long shaping for absolute length and absolute result
What should be the return value of Math.Abs (-2147483648) in Java?