Someone please explain this 🙏🏻 | Sololearn: Learn to code for FREE!

+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

9/20/2021 4:02:24 PM

MD. Ferdous Ibne Abu Bakar

10 Answers

New Answer

+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

+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!

+4

This is coz of bitwise xor operator https://www.geeksforgeeks.org/python-bitwise-operators/amp/

+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.

+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. :)

+2

Thanks mesarthim, now I understand this

+2

Abhay, No problem 🙂, my problem solved. Thanks 😀

+1

HrCoder, this link is very helpful, thanks 🙂

0

Use "**" operator. If i think right, it have to help you.

0

DiMasik23_ I knew it. ** means power. By the way thanks 🙂