Microsoft KB Archive/60868

Mod Operator Handled Incorrectly with Speed Optimization (/Ot) ID Number: Q60868

5.10 | 5.10 MS-DOS | OS/2 buglist5.10 fixlist6.00

Summary:

When using the mod operator with unsigned integers, it is possible for the compiler to generate the incorrect code when it is optimizing for speed.

More Information:

Specifically, if an unsigned variable is multiplied by a power of 2 other than 1 or 2, then the mod operator is used with a value of 256, and the incorrect number will be calculated. The following is an example that generates the incorrect number:

unsigned x = 200 ; unsigned result ;

result = (x * 4) % 256 ;

printf (“result = %u”, result) ;

This program should have 32 as the result of the operation. When compiling with /Ot (or compiling with default optimization), the result is 800. With no optimization (/Od), the result is correct (32).

Microsoft has confirmed this to be a problem with C Version 5.10. This problem was corrected in C Version 6.00.