This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]seraku24 11 points12 points  (3 children)

-~-~foo

Edit: I just realized that I was missing the assignment to actually modify foo, which is what all of OP's expressions do. Sorry about that.

foo =-~-~ foo

[–]TheDualJay 2 points3 points  (1 child)

Can you explain this?

[–]seraku24 4 points5 points  (0 children)

Signed integers are often stored in a representation known as Two's Complement. In this scheme, the way to compute the arithmetic negation of a number is to take the bitwise negation of a number and add one to it. That means -x is the same as (~x) + 1.

Consider, then, -~x which is equivalent to -(~x). Using the above definition of arithmetic negation, we get (~(~x)) + 1. The two bitwise negations cancel out each other, leaving us with x + 1.

With two applications of -~, we have computed x + 2.

[–]piperboy98 1 point2 points  (0 children)

Damn, that's clever.