-3

In Obfuscated C Code Contest 2006. Please explain sykes2.c,

there is a statement "-~i == i+1 because of twos-complement".

Can someone explain why this is the case?

Community
  • 1
  • 1
Thomas Song
  • 717
  • 4
  • 7

1 Answers1

4

-~x is equal to x+1 because ~x is equivalent to (0xffffffff-x). This is equal to (-1-x) in 2s complement, so -~x is -(-1-x) = x+1.

Thomas Song
  • 717
  • 4
  • 7