top | item 32712581

(no title)

6a74 | 3 years ago

Neat quiz. Reminds me that the absolute value of INT_MIN in C (and many other languages) is undefined, but will generally still return a negative value. This is a "gotcha" that a lot of people are unaware of.

> abs(-2147483648) = -2147483648

discuss

order

lizardactivist|3 years ago

A consequence of most, if not all, CPUs today using two's complement integers.

I think one's complement is more sensible since it doesn't have this problem, but it loses out because it requires a more complex ISA and implementation.

veltas|3 years ago

It's annoying that negation, ABS, and division can overflow with two's complement. But how I look at it: lots of operations can already overflow, just a fact of signed integers, and you need to guard against that overflow in portable code already. It doesn't seem to be fundamentally worse that those extra operations can overflow.

nayuki|3 years ago

Ones' complement (correct spelling) has negative zero, which I would argue is a far worse problem.

shultays|3 years ago

It is undefined since it involves integer overflow