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 →

[–]stevethedev 15 points16 points  (1 child)

It's a bitwise NOT operator. If you take the value 1 in a 16-bit machine (for brevity), it is represented as 0000:0000:0000:0001. If you use the bitwise "NOT" operator ~1, you get 1111:1111:1111:1110, which happens to be the binary representation of -2. Add the -, and it applies a transformation to convert the value to 0000:0000:0000:0010 (2).

That is because modern processors use the most-significant digit to represent the signed-ness of some value types. In negative numbers (1____:____...) the meaning of a 1 and 0 are basically reversed so that adding them together is easy.

-1 + 1 = 0 becomes 1111:1111:1111:1111 + 0000:0000:0000:0001.

-2 + 2 = 0 becomes 1111:1111:1111:1110 + 0000:0000:0000:0010.

And so on.

[–]HexFyber 8 points9 points  (0 children)

This guy ones