When I compile this code with VC++10:
DWORD ran = rand();
return ran / 4096;
I get this disassembly:
299: {
300: DWORD ran = rand();
00403940 call dword ptr [__imp__rand (4050C0h)]
301: return ran / 4096;
00403946 shr eax,0Ch
302: }
00403949 ret
which is clean and concise and replaced a division by a power of two with a logical right shift.
Yet when I compile this code:
int ran = rand();
return ran / 4096;
I get this disassembly:
299: {
300: int ran = rand();
00403940 call dword ptr [__imp__rand (4050C0h)]
301: return ran / 4096;
00403946 cdq
00403947 and edx,0FFFh
0040394D add eax,edx
0040394F sar eax,0Ch
302: }
00403952 ret
that performs some manipulations before doing a right arithmetic shift.
What's the need for those extra manipulations? Why is an arithmetic shift not enough?