What is tilde (~) operator in Python?


The bitwise operator ~ (pronounced as tilde) is a complement operator. It takes one bit operand and returns its complement. If the operand is 1, it returns 0, and if it is 0, it returns 1

For example if a=60 (0011 1100 in binary) its complement is -61 (-0011 1101) stored in 2's complement

>>> a=60
>>> bin(a)
'0b111100'
>>> b=~a
>>> a
60
>>>
>>> b
-61
>>> bin(b)
'-0b111101

Updated on: 18-Jun-2020

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements