Is there more encouraged ways to perform XOR operation? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Is there more encouraged ways to perform XOR operation?

I met situation where I had 2 booleans and I want only 1 of them to be true for the operation to evaluate true. By default I would stay with != comparison operator, but I think there should be a logical operator or other more encouraged way to do this, is there? (In Python or in Java)

1st Jan 2020, 5:16 PM
Seb TheS
Seb TheS - avatar
9 Answers
+ 3
Russ I used timeit, I just confused the places of operands, (^ might anyways be 3* faster than !=)
1st Jan 2020, 5:50 PM
Seb TheS
Seb TheS - avatar
+ 2
Russ I think the for loop might affect the result.
1st Jan 2020, 6:42 PM
Seb TheS
Seb TheS - avatar
+ 1
In Python: print(True ^ False) returns True, as does: print(False ^ True) so I guess that fits the bill? Can't comment on whether such use is encouraged though.
1st Jan 2020, 5:23 PM
Russ
Russ - avatar
+ 1
Russ Not more encouraged. ^ seems to be 3 times slower than != in Python, it might not surprise.
1st Jan 2020, 5:34 PM
Seb TheS
Seb TheS - avatar
+ 1
I wonder where you got "3 times worse" from - as this would appear to disagree: https://code.sololearn.com/ca439Z3hyn3O/?ref=app
1st Jan 2020, 5:41 PM
Russ
Russ - avatar
0
Seb TheS My estimate would be that ^ would be about 10% faster than !=. Not a huge difference really.
1st Jan 2020, 5:54 PM
Russ
Russ - avatar
0
Fair enough, that looks fairly conclusive. Does anyone know why I can't reach the same conclusion using my method?
1st Jan 2020, 6:24 PM
Russ
Russ - avatar
0
Seb TheS Seems like the most likely answer. Code modified to try and account for that but no luck.
1st Jan 2020, 7:04 PM
Russ
Russ - avatar