Mail Archives: djgpp/1994/11/26/03:46:21
> This program:
>
>int main() {
>
> long x = -1441992;
> unsigned y = 4;
>
> printf("%d / %d = %d\n",x,y,x/y);
>}
>
> Spits this out:
>
>-1441992 / 4 = 1073381326
Of course it does; that's how unsigned math works. You're dividing a
32-bit signed number by a 32-bit unsigned number, so C promotion rules
dictate that the signed value be promoted to unsigned before the
division takes place. A negative number promoted to an unsigned type
is a large positive number. Your example in hexadecimal is perhaps
less surprising:
0xFFE9FF38 / 0x4 = 0x3FFA7FCE
-Mat
mat AT ardi DOT com
- Raw text -