Newsgroups: comp.lang.c Path: utzoo!sq!msb From: msb@sq.sq.com (Mark Brader) Subject: Re: long conversion of short expression. Message-ID: <1989Aug1.015445.9314@sq.sq.com> Reply-To: msb@sq.com (Mark Brader) Organization: SoftQuad Inc., Toronto References: <9092@chinet.chi.il.us> Date: Tue, 1 Aug 89 01:54:45 GMT Chris Torek has already answered this at length, but I think it deserves answering briefly also. > In the following code segment, the two shorts multiplied together > exceed the size of a short, but the question is, is the result > of the multiplication really a short to be converted to a > long, or a long already? The short answer is, it's really an int to be converted to a long, but because the semantics of C on integer overflow are explicitly undefined, it is legitimate for a compiler to treat it as if it was a long already. To be safe, if there is any chance that the result of the multiplication will exceed a magnitude of 32767, and you want a long result, you should cast at least one of the operands to long. -- Mark Brader, SoftQuad Inc., Toronto, utzoo!sq!msb, msb@sq.com #define MSB(type) (~(((unsigned type)-1)>>1)) This article is in the public domain.