I saw this piece of code in the underscore.js source:
if ((!a && b) || (a && !b)) return false;
Is that equivalent to the following?
if (a ^ b) return false;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Strictly speaking, no.
&&and||are logical operators, whereas^is a bit-wise operator.But if your inputs are booleans (or integers from the set {0, 1}), then the semantics will be basically the same. If you can’t guarantee these inputs, you can still ensure identical semantics thus:
(Assuming, of course, that
aandbare plain variables, not complex expressions with side-effects.)