ABS (int_max-(-1 ))

Source: Internet
Author: User

When writing a program, the result is always wrong. Check logic has not found any errors several times, but debug is helpless. An interesting phenomenon was found: ABS (int_max-(-1) returned value is-2147483648. So I read the code implementation of the ABS function.

 
1 Int_ Cdecl ABS (2IntNumber3 )4 {5Return(Number> =0? Number :-Number );6}

the design of the unsigned number and signed number range is also related to the results after overflow. The ABS function parameter is int, which is signed. int_max-(-1) is greater than the int expression range, and its unsigned value is 2147483648, then the unsigned value is converted to a signed int of-2147483648. For the statement number> = 0? Number:-number. We get-(-2147483648), that is, 2147483648. The Return Value Type of the ABS function is int, which is signed. The value of 2147483648 is converted to-2147483648.

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.