+ 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)
9 Answers
+ 3
Russ I used timeit, I just confused the places of operands, (^ might anyways be 3* faster than !=)
+ 2
Russ I think the for loop might affect the result.
+ 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.
+ 1
I wonder where you got "3 times worse" from - as this would appear to disagree:
https://code.sololearn.com/ca439Z3hyn3O/?ref=app
0
Seb TheS My estimate would be that ^ would be about 10% faster than !=. Not a huge difference really.
0
Fair enough, that looks fairly conclusive.
Does anyone know why I can't reach the same conclusion using my method?
0
Seb TheS Seems like the most likely answer. Code modified to try and account for that but no luck.