是否有任何理由使用 (nr & 1 == 0) over (nr % 2 == 0) 来检查奇偶校验?

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

性能上有什么实际差异吗? 它更快吗? (假设我在同一个程序中至少使用了 100 次,它会提高我的程序速度吗?)

这个问题在 Software Engineering Stack Exchange 上可能更合适。

如果您使用的是优化编译器,那么任何形式的 n % <power of two> 都可能会被优化为 n & <power of two minus one>,因为它们是等效的,但在我能想到的几乎所有架构上都是后者效率更高。

前一种形式更清楚地表达了您的意图,尽管许多开发人员会将 n & 1 识别为 n % 2 的 "faster version"。