tools4j / decimal4j

Java library for fast fixed-point arithmetic based on longs with support for up to 18 decimal places.
decimal4j.org
MIT License
156 stars 17 forks source link

Why are bitwise operators used in if checks? #22

Closed v-chernyshev closed 2 years ago

v-chernyshev commented 2 years ago

Hi, I've noticed that pretty much the whole library uses & and | instead of && and || (see e.g. this example). What is the reason behind it? Unlike logical operators, the bitwise ones don't benefit from short-circuit evaluation.

terzerm commented 2 years ago

Hi, and thanks for your question. There is no real strong reason, but the idea was that these operations can be better optimised by the compiler, so they may result in marginally better performance.
See for instance here for an explanation: https://en.wikipedia.org/wiki/Short-circuit_evaluation#Reduced_efficiency_due_to_constraining_optimizations