Actually
(n%2==1)
is not the same as
(n&1==1)
if type of n
is signed int
, so the compiler code(gcc 5.1, -Ofast, 64bit):
int f(int n)
{
return (n % 2) == 1;
0: 89 f8 mov %edi,%eax
2: c1 e8 1f shr $0x1f,%eax
5: 01 c7 add %eax,%edi
7: 83 e7 01 and $0x1,%edi
a: 29 c7 sub %eax,%edi
c: 31 c0 xor %eax,%eax
e: 83 ff 01 cmp $0x1,%edi
11: 0f 94 c0 sete %al
}
14: c3 retq
So main part looks like(pseudo code):
uint64_t edi = eax;
eax >>= 0x1f;
edi += eax;
edi &= 1;
edi -= eax;
But if type of n is "unsigned int" all looks great(gcc 5.1, -Ofast):
0000000000000000 <f>:
unsigned char f(unsigned int n)
{
return (n % 2) == 1;
0: 83 e7 01 and $0x1,%edi
}
3: 89 f8 mov %edi,%eax
5: c3 retq