find me on
 
twitter del.icio.us
linkedin flickr
goodreads flixster
facebook aim
dopplr stackoverflow
tweets
 
del.icio.us
 
recent posts
 
history
 
October 2009
M T W T F S S
« Aug   Nov »
 1234
567891011
12131415161718
19202122232425
262728293031  
 
jason.prado (@) gmail.com real tangible
 
% 2 or & 1 ?
 

Should you test an integer for evenness by n % 2 == 0 or n & 1 == 0? I always assumed they compiled down to the same thing, so today I checked. All code compiled in gcc4 -O3.


(gdb) x/8i foo1
0x1fa0 <foo1>: push %ebp
0x1fa1 <foo1+1>: mov %esp,%ebp
0x1fa3 <foo1+3>: mov 0x8(%ebp),%eax
0x1fa6 <foo1+6>: leave
0x1fa7 <foo1+7>: xor $0x1,%eax
0x1faa <foo1+10>: and $0x1,%eax
0x1fad <foo1+13>: ret
0x1fae <foo1+14>: xchg %ax,%ax


(gdb) x/8i foo2
0x1fb0 <foo2>: push %ebp
0x1fb1 <foo2+1>: mov %esp,%ebp
0x1fb3 <foo2+3>: mov 0x8(%ebp),%eax
0x1fb6 <foo2+6>: leave
0x1fb7 <foo2+7>: xor $0x1,%eax
0x1fba <foo2+10>: and $0x1,%eax
0x1fbd <foo2+13>: ret
0x1fbe <foo2+14>: xchg %ax,%ax

Yep, same thing. This worked the same with signed vs. unsigned ints on my intel mac. According to this thread, that isn’t the case on all architectures.

Premature optimizers should stop prematurely optimizing. If taking the remainder when divided by 2 is clearer to you, then do it that way. The compiler doesn’t care.