assigning an integer to ...

Kok Chen kchen at Apple.COM
Wed Dec 21 03:25:04 AEST 1988


In article <1911 at pembina.UUCP> lake at alberta.UUCP (Robert Lake) writes:
>		int i;
>		short j = 1;
>		i = -(unsigned short)j;
>If I run this program on a VAX 11/780 using 4.3 BSD, I obtain -1 as the
>answer.  However, if I run this on a SUN using SUN OS 3.5, I obtain 65535
>as the answer.  Who is right?  ...

>From K&R-I, arithmetic operators (including unary ones) are supposed to 
work on longs, no? I would have compiled the line of code into:

		load j
		sign extend to a long
		negate
		(cast to type of i)
		store i

This means BSD is the more correct one, based on K&R-I.

But, why would one have written such a contorted line of code in real life?
Or, is this yet another attempt at breaking compilers? :-)


Kok Chen			{decwrl,sun}!apple!kchen
Apple Computer, Inc.



More information about the Comp.lang.c mailing list