Puzzle on unsigned promotions
Jeffrey E. F. Friedl
jeff at unh.UUCP
Fri Jul 1 23:52:21 AEST 1988
In article <12251 at mimsy.UUCP>, chris at mimsy.UUCP (Chris Torek) writes:
> In article <736 at vsi.UUCP> friedl at vsi.UUCP (Stephen J. Friedl) writes:
> >... unsigned- vs value-perserving rules ...
^^^^^^^^^^
(personal note to Steve: you misspelled this word.
Did you mean "preserving", or "perversing" here? [(-8])
> []
>
> Now, to answer `what should I get', you need to know the relative
> sizes of short, int, and long (and their unsigned variants).
[]
> sval = 0xffff; sizeof(short) = 2; sizeof(int) = sizeof(long) = 4:
>
> lval = -sval:
>
> Old rule: unsigned stays unsigned
> First turn sval into an rvalue: (u_short)0xffff is
> extended to (u_int), result is (u_int)0x0000ffff
Here's (I think) the problem (I'm being a devil's advocate here.. keep
the flame level low while you read).... I would *love* to be proven
wrong on this, but no where in K&R-I can I find where it specifically
stipulates that the conversion from unsigned short to a larger unsigned
int is done by zero padding and not sign (sign with an unsigned?) extending.
The main problem is K&R's use of terms and fonts. In some places, when
they mean to say INT (i.e. 'int' in compu-font), they say 'integral'
and vicea-verca. I can find quotes (taken from context) that support
both the thought that the sign should be extended and that it shouldn't
be.
Of course, it makes sense that it shouldn't be, but the language definition
(K&R-I) is wishy-washy about it.....
Specifically, see K&R-I, section 6.1 (page 183):
"A ... short integer may be used wherever an integer may be used.....
Conversion of a shorter integer to a longer always involves sign
extension; integers are signed quantities"
No, they're not. (I hear flames being set to "cinderize"....)
"UNSIGNED SHORT INT" is a "shorter integer" but is not signed.
If they had meant to exclude unsigned quantities, they should have said it.
But then, if they excluded unsigned numbers, that would mean to say that
unsigned shorts may NOT be used wherever an integer may be used.
So, it seems that in the above quote, the first two uses of the word "integer"
referred to both signed and unsigned quantities, and the later uses of the
word refer to signed quantities only.
K&R-I, section 6.5 (page 184), 2nd paragraph:
"When an unsigned integer is converted to LONG, the value of the
result is the same numerically as that of the unsigned integer"
My problem with the above quotes is that in the first they say "integer"
rather than "INT", "SHORT", or "LONG".
They say "LONG", not something like "unsigned integer" which I think they
should.
So, when converting from a 2 byte u_int to a 4 byte int, do you
u_int 0xffffffff or 0x0000ffff (the latter being the most natural)?
-----------------------------------------------------------------
How about this: when sizeof(short, int, long) == (2,2,4) under rules
given int both K&R-I and K&R-II,
(long)(unsigned short)0xffff
is
signedlong 0x00000001
(I think. I worked through it quite carefully -- If I made a mistake,
I'm sure a dozen kind souls will let me know [(-:] )
> >but on the HP9000 I see:
> >
> > lval #1 = 1 #2 = -65535
>
> Your HP9000 compiler is broken.
But it's only half boken. "Two (different) answers are better than one!"
Well.... Maybe not.
> >Please email, I'll summarize and post.
>
> Oops :-)
Oops :-)
"Scotty, we've got to *flame*. We *need* more energy"
"Ay, I'm trying as hard as I can, cap'an"
*jeff*
-------------------------------------------------------------------------------
Jeffrey Eric Francis Friedl, Box 2146 Babcock House, Durham New Hampshire 03824
..!{uunet,decvax}!unh!jeff j_friedl at unhh.bitnet ..!ucbvax!kentvax!jfriedl
I hope I'm not around Jan 18, 2038 at 10:14:08PM
Nancy Reagan on Steven Friedl: just say "hiho"
More information about the Comp.lang.c
mailing list