[pe.cust.bugs] short casts, and long = {short,char} = long, don't work

msb@lsuc.UUCP (Mark Brader) (03/10/85)

We run Edition VII on a 3220.
This program:

	#include <stdio.h>

	main()
	{
	long l[5];
	short s;

		l[0] = 0x234567;
		l[1] = s = l[0];
		l[2] = s;

		l[3] = (short) l[0];
		l[4] = (short) 0x234567;

		for (s = 0; s < 5; ++s)
			printf ("l[%d] = %x\n", s, l[s]);

		printf ("\n(short)0x234567 = %x\n",
			   (short)0x234567);

		printf ("\nsizeof (short) = %d\nsizeof (long)  = %d\n",
			   sizeof (short),      sizeof (long));
	}

should output 4567 for l[1] through l[4] and for the cast in the second-last
printf.  In fact, the result is:

	l[0] = 456789
	l[1] = 456789
	l[2] = 6789
	l[3] = 456789
	l[4] = 456789

	(short)0x456789 = 456789

	sizeof (short) = 2
	sizeof (long)  = 4

If char is substituted throughout for short, the casts work, but l[1]
(long = char = long) is still wrong.  The output then is:

	l[0] = 234567
	l[1] = 234567
	l[2] = 67
	l[3] = 67
	l[4] = 67

	(char)0x234567 = 67

	sizeof (char) = 1
	sizeof (long)  = 4

In case anyone doesn't believe that l[1] is wrong in both the above
examples, they should consider this program:

	#include <stdio.h>

	main()
	{
	long l1, l2;
	float f;

		l1 = f = 123456789;
		l2 = f;
		printf ("%d\n%d\n", l1, l2);
	}

This correctly produces:

	123456784
	123456784

Notice the loss of precision from the type conversions.  Of course, an
assignment is an expression whose type is that of the variable assigned to.

I discovered the bug while debugging my signature line below.
It doesn't work on this machine for type short!

{ allegra | decvax | duke | ihnp4 | linus | watmath | ... } !utzoo!lsuc!msb
			    also via { hplabs | amd | ... } !pesnta!lsuc!msb
Mark Brader		    and			   uw-beaver!utcsri!lsuc!msb

				#define msb(type) (~(((unsigned type)-1)>>1))

msb@lsuc.UUCP (Mark Brader) (03/10/85)

I said:

> We run Edition VII on a 3220.
> This program:
>	......
> should output 4567 for l[1] through l[4] and for the cast in the second-last
> printf.

Of course I meant 6789, not 4567.

Mark Brader