What is ~ ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

What is ~ ?

In quiz rating I found: x=~3 y=5 print(x*y) So I made this code but I still dont know what it is "~" https://code.sololearn.com/cBV4zBBVPT2O/?ref=app

4th Sep 2019, 8:20 PM
Sousou
Sousou - avatar
8 Answers
+ 8
It's the Bitwise Complement operator. https://www.sololearn.com/learn/4076/
4th Sep 2019, 8:36 PM
Diego
Diego - avatar
+ 7
an int is a number that is not a decimal
4th Sep 2019, 8:21 PM
JT JT
JT JT - avatar
+ 4
~ x Returns the complement of x - the number you get by switching each 1 for a 0 and each 0 for a 1. This is the same as -x - 1. This operator takes a number’s binary, and returns its one’s complement. For this, it flips the bits until it reaches the first 0 from right. https://code.sololearn.com/cv5I6VanqJWD [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [-1, -2, -3, -4, -5, -6, -7, -8, -9, -10] [0, -1, -2, -3, -4, -5, -6, -7, -8, -9] i binary(i) binary(~i) ~i 0 0000 -001 -1 1 0001 -010 -2 2 0010 -011 -3 3 0011 -100 -4 4 0100 -101 -5 5 0101 -110 -6 6 0110 -111 -7 7 0111 -1000 -8 8 1000 -1001 -9 9 1001 -1010 -10
6th Sep 2019, 4:38 PM
Sgk101
Sgk101 - avatar
+ 3
JT JT I mean what is "~"
4th Sep 2019, 8:33 PM
Sousou
Sousou - avatar
+ 2
Thx
4th Sep 2019, 8:38 PM
Sousou
Sousou - avatar
+ 2
It is actually the Bitwise Complement operator. Check ot pit here(Learn About It)- https://www.sololearn.com/learn/4076/
6th Sep 2019, 9:20 AM
Pratham0607
Pratham0607 - avatar
+ 2
It's used as a "reverse" or "opposite" opperation for integers. So say x = 3, and then we do ~x, thats equivalent to -3
6th Sep 2019, 2:49 PM
livsprad
+ 1
Actuall it does this number = -number - 1 If the number is positive number it will turn it to negative and subtract 1 from it. If the number is negative number it will turn it into a positive number then subtract 1 from it.
4th Feb 2021, 8:23 AM
K.S.S. Karunarathne
K.S.S. Karunarathne - avatar