Bitwise NOT

In short: The bitwise NOT operator (~) flips every bit of a number, turning each 1 into a 0 and each 0 into a 1. With two's complement representation, this means ~n equals -(n + 1).

The NOT bitwise operation inverts bits. A 0 becomes a 1. A 1 becomes a 0.

The NOT operator is often written as a tilde character ("~"):

~ 0000 0101 = 1111 1010

When numbers are printed in base-10, the result of a NOT operation can be surprising. In particular, positive numbers can become negative and negative numbers can become positive. For example:

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

This is because numbers are (usually) represented using two's complement, where the leftmost bit is actually negative. So flipping the leftmost bit usually flips the sign of the number.

Frequently Asked Questions

What does the bitwise NOT operator do?

It inverts every bit of its operand: each 1 becomes 0 and each 0 becomes 1.

Why does ~n equal -(n + 1)?

Because integers use two's complement: flipping all bits of n gives the bit pattern for -(n + 1). For example, ~5 is -6.

What's the difference between ~ and !?

~ is the bitwise NOT that flips the bits of an integer; ! is the logical NOT that turns a true value into false and vice versa.

Last updated: June 17, 2026

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

. . .