Rarely I bump into & and | logic operators in other's codes instead of || and &&. I made a quick research because I never used them and didn't know what is it for.
A && B means if A is false, B won't be evaluated and it will return false.
A & B means if A is false, B will be evaluated even if the form will return false as well.
Of course it is the same game with | and ||.
My question is: does it make sense anyways to evaluate the second member if the first determine the evaluation? I can imagine a code where the second member does something super important logic in clode and so it should be evaluated, but I suspect it is bad practice. Is it enough to make | and & exist?
from a comment, to make it more clear: I have the feeling that the program is not optimal and working well or better to say: designed well if all of the logical members HAS to be evaluated.
You forgot about bitwise operations
The bitwise & operator performs a bitwise AND operation.
The bitwise | operator performs a bitwise inclusive OR operation.
But here you can find example when unconditional AND operator is better:
What are the cases in which it is better to use unconditional AND (& instead of &&)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With