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
0x8000.. is stored as 10000.... (binary). This is known as twos complement, which means that the highest bit (the one at the left) is used to store the sign of the value and negative values are stored with negative binary - 1. The abs() function now checks the signbit, sees that it is set and computes the positive value.
Now this is a negative number again which we didn't want, the reason is a overflow, try the number 0x9000... which is 10010...
With this number the overflow is stopped by the 0 bit on the right