Should I check for parity of a number in a different way?
N % 2 == 0 的技巧来获取一个数字的奇偶性，但我相当肯定在时间复杂度方面存在更好的解决方案。
N & 1 == 0 可能已经更好了，但它是否在常数时间内运行，即它是否只进行右侧比特的比较，还是会处理N的所有log(N)位组成？
In Java, as well as other languages, I always use the
N % 2 == 0 trick to obtain the parity of a number, but I'm pretty sure that in terms of time complexity there is some better solution.
It's probably better already by using
N & 1 == 0, but does this run in constant time, i.e. does it just do the rightmost bit comparison, or does it do all of the log(N) bits composing N?
Is it possible to somehow just read the last bit of the number, which would be in constant time for sure?
编译器编写者非常了解除法和取余指令的昂贵，会尽一切可能避免生成使用这些指令的代码 - 详见例如 https://stackoverflow.com/questions/41183935/why-does-gcc-use-multiplication-by-a-strange-number-in-implementing-integer-divi 很可能，但不能保证，
& operator is implemented in hardware circuitry. The circuitry runs over all the bits of the number in parallel in one single step. It does not look at bits one by one.
Integer division and remainder operations on the other hand are a lot more complex. Popular CPUs don't have special circuitry for it, instead it's implemented in microcode. It's hard to find a primary source, but different sources on the internet put integer division at about 20-40 times as expensive as bitwise operators like
Compiler writers are well aware how expensive the division and remainder instructions are, and do everything possible to avoid generating code that uses these instructions - see for example https://stackoverflow.com/questions/41183935/why-does-gcc-use-multiplication-by-a-strange-number-in-implementing-integer-divi It is very likely, but not guaranteed, that
N%2==0 is compiled to
N&1==0. To make sure, you would have to examine the machine code generated by the compiler. In Java, the compiler you should most care about is the JIT compiler. It generates native machine code on the fly. Examining its output is possible, but not easy. Also, the optimizations it does may change from one version to the next, or vary between CPU models.
If you really want to know which is faster in your application with your hardware, you should create a benchmark and test it yourself.