~ Bitwise NOT
Last updated: 2026-09-22
~ Bitwise NOT
~ inverts all bits: ~x == -(x+1).
| Category | Operators |
|---|---|
| Kind | Operator |
| Python Version | all |
📝 Syntax
~a
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
-1
-6
2
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QWhy does ~5 equal -6?
ABecause `~x == -(x+1)`: `~` flips all bits (including the sign bit), which under two's complement inevitably gives that result. `~0 == -1`, and `~-3 == 2`.
QWhat is ~ useful for?
AA common trick is to get the «(i+1)-th from the end»: `lst[~i]` is equivalent to `lst[-i-1]`, e.g. `lst[~0]` takes the last element. Bit flipping is also used in some algorithms.
QIs ~ logical negation?
ANo. Logical negation is `not` (which returns a bool); `~` is bitwise NOT (which returns an int). `not 5` is `False`, but `~5` is `-6`.