Hacker News new | ask | show | jobs
by jonsen 3412 days ago
15 + 15 gives 14

14 - 15 gives 15

1 comments

The way I wrote this was rubbish, edited. I was trying to get at if you have 1110, 1111 then 1110 + 1111 will overflow, but you could xor them.

  1110 + 1111 = 1101
  1101 - 1110 = 1111
  1101 - 1111 = 1110
Overflow doesn't matter if you just want to swap.
(Assuming two's complement arithmetic, as amelius pointed out - I don't know if all programming languages and platforms use it)
That's a really smart observation I hadn't made, thanks for explaining!