Shifting question

Guy Harris guy at gorodish.Sun.COM
Thu Jul 21 16:26:40 AEST 1988


> Contrary to the other two follow-ups I've seen (when will
> people learn not to react without thinking), yes you do have a
> valid complaint. The C language (both K+R and X3J11)
> guarantees that X >> n is a logical shift, with 0 padding on
> the left, when X is an unsigned quantity, as it is in this
> case. It is only when X is not unsigned, that a right shift is
> implementation dependant.

I don't know which follow-ups *you* saw; none of the ones *I* saw said
*anything* about smearing the sign bit.  All the ones I saw pointed out,
quite correctly, that he did *not* have a valid complaint, because the result
of shifting a signed *or* unsigned 32-bit quantity right by 32 bits is
undefined.

It might arguably be more correct to say that complaining that his compiler
wasn't "doing the right thing" according to some language specification wasn't
valid.  He could, I suppose, complain that the language specification should
say what happens when you shift by more than the size of the LHS, in bytes, or
that the hardware shouldn't take the shift count modulo 32, or that the
compiler should "do the right thing" even though it's not required by the
language specification; these might or might not be considered valid complaints
depending on your point of view.



More information about the Comp.lang.c mailing list