Do these two statements compile equivalently: n % 2 == 0 and n & 1 == 0 ?
if not, is one more efficient?
No, they do not always give the same result. The C standard allows for ones' complement implementations, in which case they will give a different result for negative n
.
A similar question was asked yesterday.
i % 2
and i & 1
are not the same:
n & 1
does not necessarily indicate whether your integer is even or odd.i % 2
can be negative.For unsigned integers, a good compiler should reasonably produce equally efficient code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With