Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the squiggle (tilde) i.e. `~` operator do in Python? [duplicate]

Possible Duplicate:
The tilde operator in Python

What does the squiggle i.e. ~ operator do in Python?

(This is probably Python 101 for most, but I came across ~ in code and had no idea what it was, and it took me a surprising amount of research to figure it out, so hopefully this Q&A can help someone down the road.)

like image 566
Ghopper21 Avatar asked Aug 02 '12 16:08

Ghopper21


1 Answers

It's the unary bitwise invert operator.

The unary ~ (invert) operator yields the bitwise inversion of its plain or long integer argument. The bitwise inversion of x is defined as -(x+1). It only applies to integral numbers.

like image 64
Ghopper21 Avatar answered Sep 28 '22 05:09

Ghopper21