What should be the return value of Math.Abs (-2147483648) in Java?

Source: Internet
Author: User
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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.