I saw the chosen answer to this post.
I was suprised that (x & 255) == (x % 256)
if x is an unsigned integer, I wondered if it makes sense to always replace %
with &
in x % n
for n = 2^a (a = [1, ...])
and x being a positive integer.
Since this is a special case in which I as a human can decide because I know with which values the program will deal with and the compiler does not. Can I gain a significant performance boost if my program uses a lot of modulo operations?
Sure, I could just compile and look at the dissassembly. But this would only answer my question for one compiler/architecture. I would like to know if this is in principle faster.
question from:https://stackoverflow.com/questions/40759800/in-special-cases-is-faster-than