How is the modulo operator implemented in the HotSpot JVM?
问题 I understand that the modulus operation can be optimised using a little & wise magic where the divisor is a power of 2... and presumably this an optimisation that the JIT compiler makes? 回答1: Yes, a modulo x % pow(2, n) can be achieved using x & ((1 << n) - 1) But the %-operator in java can give different results if x is negative, so blindly substituting one for the other can break code. When bit-addressing, masking etc. the &-variant is commonly used, as its semantically closer to what is