Computer >> Computer tutorials >  >> Programming >> Python

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