And even C#: https://godbolt.org/z/5WPfT8e5G Does use the &-2 strategy.
Dart also uses and: https://godbolt.org/z/1437df4En
So most of languages compiled to native binaries seem to do it. Languages like Ruby and Python don't seem to optimize this, which is hardly surprising. Ruby has a JIT now, and the JIT might do it.
And I'm not sure what the haskell version does, can someone explain: https://godbolt.org/z/xPP4E1a6h ?
Without -O, what's happening is that "mod" is a method from the Integral type class (for the non-haskellers: read "interface"/"abstract class"), and thus the program just reads the corresponding field from the stafically allocated copy of the Integral dictionary for Int, and subsequently calls it. Of course one should just inline this known function call, and that's precisely what -O lets ghc do.
Btw, it's a lot simpler if you use only unsigned ints. Both GCC and clang add a couple more instructions to make it work with negative numbers. With unsigned ints, both gcc and clang generate this simple assembly code:
is_even:
mov eax, edi
and eax, 1
retSo the easiest answer to the original question (whether a number is even or odd) likely involves &1, not %2. Note taken.
What you want to return is a boolean. And in that case the code generated is much simpler https://godbolt.org/z/5dq7MnrEx