Python challenge question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Python challenge question

What is the output of this code? x=~3 y=5 print(x*y) Result -20 Why?

13th Nov 2021, 5:47 AM
Sanjar Egamberdiyev
Sanjar Egamberdiyev - avatar
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.)
13th Nov 2021, 6:29 AM
Python Learner
Python Learner - avatar
+ 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!
13th Nov 2021, 1:24 PM
US Department Of State
US Department Of State - avatar