Why is abs(0x80000000) == 0x80000000?

前端 未结 9 1433
心在旅途
心在旅途 2021-02-07 02:51

I just started reading Hacker\'s Delight and it defines abs(-231) as -231. Why is that?

I tried printf(\"%x\", abs(0x80000000)) on a f

9条回答
  •  滥情空心
    2021-02-07 03:10

    Actually, in C, the behavior is undefined. From the C99 standard, §7.20.6.1/2:

    The abs, labs, and llabs functions compute the absolute value of an integer j. If the result cannot be represented, the behavior is undefined.

    and its footnote:

    The absolute value of the most negative number cannot be represented in two’s complement.

提交回复
热议问题