Someone please explain this 🙏🏻 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Someone please explain this 🙏🏻

from math import * print(5^2) #output: 7 print(1^2) #output: 3 print(3^2) #output: 1 print(7^2) #output: 5

20th Sep 2021, 4:02 PM
Fꫀⲅძ᥆͟ᥙ᥉᯽
Fꫀⲅძ᥆͟ᥙ᥉᯽ - avatar
10 Answers
+ 10
This is "^" an assignment operator in Python. It is used to perform Bitwise XOR on the operands and then assigning result to the left operand. https://code.sololearn.com/cqI8ecK0sEOE/?ref=app Also, look at this, (from Bahha🐧 ) [ how the result is calculated? 5^3 = 6 And the others] ^ is for xor you have to think of numbers as binary. that's why they are called bitwise operators. in xor if you get the same digit you get 0 example. 0 ^ 0 = 0 1 ^ 1 = 0 0 ^ 1 = 1 1 ^ 0 = 1 in your example read them vertically 5 = 00000101 xor ^ 3 = 00000011 = = 00000110 00000110 binary = 6 decimal
20th Sep 2021, 4:10 PM
mesarthim
mesarthim - avatar
+ 6
DiMasik23_ "**" is an arithmetic operator and it is used for exponentiation in Python. But we use "^" as Bitwise operator and it is not used for exponentiation. "^" not defined for exponentiation in Python. As I know, "^" defined for exponentiation in R. Thanks for understanding. Happy coding!
20th Sep 2021, 6:28 PM
mesarthim
mesarthim - avatar
20th Sep 2021, 4:09 PM
Pariket Thakur
Pariket Thakur - avatar
+ 4
mesarthim what do you mean by "^" Is an assignment operator? And don't we perform bitwise right shift using ">>" operator ? Sorry if i asked something stupid but just clearing my doubts.
20th Sep 2021, 4:29 PM
Abhay
Abhay - avatar
+ 2
Abhay ">>" also using for shifting but in Python, there is a special sign its name is "Bitwise XOR". The sign looks the visualization binary expansions of the variables and does some calculations in binary dimension. I hope, it's clear enough. Also, don't think that it's stupid, I can be wrong or mix something. So, asking is the best choice. :)
20th Sep 2021, 5:16 PM
mesarthim
mesarthim - avatar
+ 2
Thanks mesarthim, now I understand this
20th Sep 2021, 11:59 PM
Fꫀⲅძ᥆͟ᥙ᥉᯽
Fꫀⲅძ᥆͟ᥙ᥉᯽ - avatar
+ 2
Abhay, No problem 🙂, my problem solved. Thanks 😀
21st Sep 2021, 12:05 AM
Fꫀⲅძ᥆͟ᥙ᥉᯽
Fꫀⲅძ᥆͟ᥙ᥉᯽ - avatar
+ 1
HrCoder, this link is very helpful, thanks 🙂
21st Sep 2021, 12:03 AM
Fꫀⲅძ᥆͟ᥙ᥉᯽
Fꫀⲅძ᥆͟ᥙ᥉᯽ - avatar
0
Use "**" operator. If i think right, it have to help you.
20th Sep 2021, 6:20 PM
DiMasik23_
0
DiMasik23_ I knew it. ** means power. By the way thanks 🙂
21st Sep 2021, 12:07 AM
Fꫀⲅძ᥆͟ᥙ᥉᯽
Fꫀⲅძ᥆͟ᥙ᥉᯽ - avatar