Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
print (3^4)
How the above line executing. Kindly explain how it works?

What I have tried:

print (3^4)
How the above line executing. Kindly explain how it works?
Posted
Updated 28-Mar-21 5:16am

The ^ char is a logical XOR operator. It compares the individual bits in both arguments, it returns 1 if one of the bits is and the other is 0.
     3 = 0011
     4 = 0100

Answer = 0111
 
Share this answer
 
That is the bitwise XOR operator, which operates on binary values.
3 decimal is 0011 binary
4 decimal is 0100 binary
The XOR operator matches when one number has a 1 and the other has a zero in a position.
So an XOR of 0011
         and 0100
             ====
       gives 0111 which equals 7 in decimal.


See operator — Standard operators as functions — Python 3.9.2 documentation[^] for full details.
 
Share this answer
 
v2
Quote:
How the above line executing. Kindly explain how it works?

Python
print (3^4)
#       ^ anything at this place is an operator

Look at documentation: Python Operators[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900