conversion of short to unsigned int

dmr at dutoit.UUCP dmr at dutoit.UUCP
Wed Mar 27 15:25:47 AEST 1985


Mike Wescott wondered why, after

	unsigned short us = -3;
	short s = -3;

the comparison

	(unsigned int)s == us

should yield true (this, evidently, on a VAX).  The reason is that
his compiler, along with those of a great many people,
has a bug.  The short  s  should indeed promote to 0xfffffffd (an int)
and then be cast to unsigned (same bits, in 2's complement), and compare
unequal with the 0xfffd stored in  us .

This is another instance where a reasonably clear, if complicated,
description in the manual appears hopelessly confused because the
compiler doesn't implement the manual.

Incidentally, Ken Turkowski's remarks about casts: "... a cast, saying
that  s  is considered unsigned rather than signed.  It [a cast] is NOT
a conversion"  are quite wrong.  Casts specify conversions, not
requests to reinterpret a variable as containing some type other than
its own.  Many casts, especially those involving unsigned, or pointers,
do indeed not actually change any bits in the value.  This fact may have misled
Ken, and probably also the compiler writer.  The way to think of casts
is to imagine assigning their operands to temporary variables with
the type specified by the cast, and then using the temporary in the larger
expression in which the cast occurs.

	Dennis Ritchie



More information about the Comp.lang.c mailing list