Note that the results of shifting of signed integers (especially RIGHT shifting) are poorly defined (or explicitly NOT defined.)
Make sure you make your "long" is unsigned!
unsigned long rightshift(unsigned long a, char bits)
{
return(a>>bits);
}
Generates exactly the code I'd expect; lsr/ror/ror/ror inside a loop...