Cross-platform way to pack (int + flags) to unsigned int


P

Philip Lantz

Tim said:
Actually there's a big difference. It may be rare that the
difference has a significant effect, but it can. Doing a left
shift of a negative value can easily produce a trap representation
(obviously only in implementations that have trap representations
for signed integers). This may not occur often, but certainly it
is not unheard of. So the 'undefined behavior' consequences are
not just imaginary. By contrast, a right shift of a negative
value must produce some valid value -- it can't just blow up the
way a left shift of negative values can.

Yes. All I meant by "no difference" was that neither one can be used in
portable code. (My impression is that most contributions to this
newsgroup focus on portable code, which is why I didn't feel the need to
clarify that. I suppose there might be some portable usage of a signed
right shift that can deal with the fact that the result may vary, but
that seems a bit unlikely.)
 
Ad

Advertisements

T

Tim Rentsch

Philip Lantz said:
Yes. All I meant by "no difference" was that neither one can be
used in portable code. (My impression is that most contributions
to this newsgroup focus on portable code, which is why I didn't
feel the need to clarify that. I suppose there might be some
portable usage of a signed right shift that can deal with the fact
that the result may vary, but that seems a bit unlikely.)

I understand that you have a point to make, and even sort of
agree with it; my complaint is it wasn't expressed very well.
Still isn't, unfortunately. Almost every C program ever written
relies on implementation-defined behavior to some degree,
including programs considered "portable". What makes you think
the implementation-defined aspects of right shift are somehow
more extreme than the other kinds of implementation-defined
behaviors considered generally acceptable? What's more, as a
blanket statement what you say is wrong: certainly it is
possible to use right shift of negative values, even in a
non-trivial way, in portable code. Of course many uses won't be,
but that's not nearly the same as saying none ever are.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top