No, ~
has nothing to do with interpreting the number as negative: tilde ~
operator interprets the number as a pattern of bits, which it then inverts (i.e. replaces zeros with ones and ones with zeros). In fact, if you apply ~
to an unsigned value, the result would remain positive.
Recall that 1 << k
expression produces a pattern of all zeros and a single 1
at the position designated by k
. This is a bit mask that can be used to force bit at position k
to 1
by applying OR operation.
Now consider what happens when you apply ~
to it: all 0
s would become 1
s, and the only 1
would become zero. Hence, the result is a bit mask suitable for forcing a single bit to zero by applying AND operation.