~ Bitwise NOT

Last updated: 2026-09-22

~ Bitwise NOT

~ inverts all bits: ~x == -(x+1).

CategoryOperators
KindOperator
Python Versionall

📝 Syntax

~a

▶ Example

Edit the code, press Run, and see the result in the output panel:

Output:

-1
-6
2
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`.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏