I am confused as to when I should use Boolean vs bitwise operators
andvs&orvs|
Could someone enlighten me as to when do i use each and when will using one over the other affect my results?
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.
Here are a couple of guidelines:
The short-circuiting behaviour is useful in expressions like this:
This would not work correctly with the bitwise
&operator because both sides would always be evaluated, givingAttributeError: 'NoneType' object has no attribute 'foo'. When you use the booleanandoperator the second expression is not evaluated when the first is False. Similarlyordoes not evaluate the second argument if the first is True.