I have seen people use addition where a bitwise OR would be more conceptually appropriate, because they believe it is faster. Is this true? If yes, do all modern compilers know this trick?
Share
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.
“Conceptually appropriate” and “faster” are two different things. The former is semantics, while the latter often involves breaking semantics.
As for the question in the title, there’s very little (if any) difference speedwise. A compiler for a CPU where this actually happens, will usually optimize it anyway — if it doesn’t cause different results, which it very well can and usually will.
Write your code correctly — if you mean to OR, then OR. If the add-vs-OR ends up being faster, either your compiler will do it for you or you can change it later after you’ve decided whether the potential extra half-nanosecond per iteration is worth the cost in readability and the bugs such a change might cause.