long conversion of short expression.

Mark Brader msb at sq.sq.com
Tue Aug 1 11:54:45 AEST 1989


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 at sq.com
#define	MSB(type)	(~(((unsigned type)-1)>>1))

This article is in the public domain.



More information about the Comp.lang.c mailing list