www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1994/11/26/03:46:21

Date: Fri, 25 Nov 94 22:11:37 -0700
From: "Mathew J. Hostetter" <iclone!mat AT sloth DOT swcp DOT com>
To: djgpp AT sun DOT soe DOT clarkson DOT edu
Subject: -1441992/4 = 1073381326
Cc: OKRA AT max DOT tiac DOT net

>  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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019