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
This goes back to how numbers are stored.
Negative numbers are stored using two's complement. The algorithm goes like ...
Flip all the bits, then add 1.
Using eight bit numbers for examples ...
+0 = -0
00000000 -> 11111111, 11111111 + 1 = 100000000
(but due to limitation of bits, this becomes 00000000).
AND...
-128 [aka -(2^7)] equals -(-128)
10000000 -> 01111111, 01111111 + 1 = 10000000
Hope this helps.