Why is it that the bitwise NOT operator (~ in most languages) converts the following values like so:
~
-2 -> 1 -1 -> 0
-2 -> 1
-1 -> 0
This is due to how negative numbers are represented as bits. For this most commonly Two's Complements are used.
-2 happens to be 1111110 in this notation, which negated is 1