Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> Much better :) But what about C? Let’s try it:

> I tried both versions (modulo 2 and bitwise AND) and got the same result. I think the optimizer recognizes modulo 2 and converts it to bitwise AND.

Yes, even without specifying optimizations - https://godbolt.org/z/9se9c6qKT

You can see that the output of the compiler is identical whether you use `i%2 == 0` or `(i&1) == 0`. The bitwise AND is instruction 12 in the output.

Using -O3 like in the post actually compiles to SIMD instructions on x86-64 - https://godbolt.org/z/dWbcK947G



With i < 71, the compiler will just turn it into a constant value of 36. Switches to SIMD at 72, idk why.


That's how you check modulus for powers of 2. 2 is a power of 2. This barely even qualifies as an "optimization".




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: