+ 5
Python challenge question
What is the output of this code? x=~3 y=5 print(x*y) Result -20 Why?
2 Answers
+ 11
Hi Sanjar!
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= 10 (0000 1010 in binary) its complement is -11 (-1111 0101) stored in 2's complement.
Here, x = ~3(0000 0011) = -4(-1111 1100)
So, -4*5 = -20
(The general formula to calculate the tilde operation ~i is ~i=-i-1.)
+ 7
JUMP_LINK__&&__Python__&&__JUMP_LINK Learner Thank you! While that was not my goal when I opened this page, I have learned a lot after reading your comment!