This is called the two's complement arithmetic. You can read about it in more detail here.
The operator ~
is a binary negation operator (as opposed to boolean negation), and being that, it inverses all the bits of its operand. The result is a negative number in two's complement arithmetic.