Is there any reason to use (nr & 1 == 0) over (nr% 2 == 0) to check for parity?

Is there any actual difference in performance? It's faster? (let's say I use it in at least 100 cases in the same program, will my program improve in terms of speed?)

+6
source share
1 answer

This question may be more relevant in the Software Stack Package .

If you use an optimizing compiler, the likelihood that any form of n % <power of two> will be optimized for n & <power of two minus one> is anyway, since they are equivalent, but pretty much every architecture I'm talking about I can think is much more effective.

The first form expresses your intentions more clearly, although many developers recognize n & 1 as the "faster version" n % 2 .

+5
source

Source: https://habr.com/ru/post/1013653/


All Articles