I know the principle of a&1
:
for example:
4&1: 0100
0001
= 0000
and
5&1: 0101
0001
= 0001
But I want to know what and how is the principle of a%2
Which is faster, a%2
or a&1
?
I know the principle of a&1
:
for example:
4&1: 0100
0001
= 0000
and
5&1: 0101
0001
= 0001
But I want to know what and how is the principle of a%2
Which is faster, a%2
or a&1
?
There are two cases:
x % 2
as x & 1
even without the optimizations. So the speed will be the same as both will have the same machine code.x % 2
and result of x & 1
are completely different and in this case you should use x % 2
to get modulo and x & 1
to test the LSB. https://godbolt.org/z/cEY4dM