Bitwise NOT

The NOT bitwise operation takes one set of bits, and for each bit returns 0 if the bit is 1, and 1 if the bit is 0.

~ 0 → -1 ~ 1 → -2

When performing NOT on an integer, each bit of the integer is inverted.

~ 5 # gives -6 # At the bit level: # ~ 0000 0101 (5) # = 1111 1010 (-6)

If you're unsure why the resulting number is negative in this example, it's because numbers are represented using two's complement. Read up on binary numbers here.

See also:

Pass Your Interviews with My FREE 7-Day Crash Course

I'll teach you the right way of thinking for breaking down tricky algorithmic coding interview questions you've never seen before.

No prior computer science training necessary—I'll get you up to speed quickly, skipping all the overly academic stuff.

No spam. One-click unsubscribe if you hate it.

What's next?

If you're ready to start applying these concepts to some problems, check out our mock coding interview questions.

They mimic a real interview by offering hints when you're stuck or you're missing an optimization.

Try some questions now

Psst. Pass it on.

. . .