+ 1

What does ^ operator mean in calculation?

# I accidentally tried the following code in python: print(3^2) print(2^3) print(3^2==2^3) """ output 1 1 True """ # However, after trying more codes and googling, I still didn't find out the meaning of this operator. # I know the ^ means XOR in boolean used in OOP, but this doesn't explain what happend in the codes above. # Does anyone know what ^ means in calculation?

13th Aug 2017, 12:39 PM
李立威
2 Réponses
+ 20
5^6 (first converted to binary) 101^110 (one or other but not both or none) 101 110 == 011 = 3
13th Aug 2017, 12:50 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 3
It looks at two elements down to the binary level. Bitwise operators works on bits and performs bit by bit operations. ^ copies the bit if it is set in one operand but not both. Assume a = 60, b = 13 in binary this is: a = 0011 1100 b = 0000 1101 When using operation: a^b = 0011 0001 If you compare both a and b, you'll notice it flips the value if both aren't the same. The last four in both are: 1100 1101 Since 110, and 110 are the same, they aren't copied over to the new binary set. So ... >>>0001 next is 0011 and 0000. Since 00 is the same in both a and b. 11 is copied to the new binary set. >> 0011 0001 If you still don't understand. Basically it creates a binary set of 0's. It looks at both elements (in binary form), and copies any bit that isn't identical in the same index as the other. Hopefully my explanation helps, if someone needs to correct me. Please do so if I made a mistake anywhere.
13th Aug 2017, 12:52 PM
Sapphire