On 2006-07-27, Snis Pilbor <> wrote:
> Hello,
>
> First question: besides the syntactical difference of one having
> to be enclosed in brackets in some situations, are there any subtler
> differences between (say)
> x=(some expression), y=(some expression);
> and
> x=(some expression); y=(some expression); ?
>
Well, the first is a single expression returning the value of y. The
second is two separate expressions and can't be used as, say,
c = (x=a, y=b); which gives c the value of b.
> What about sequence points? For example, would the phrase "i++, i++;"
> be well-defined? Is there any guarantee about which computation the
> program will perform first, for example is "x=x+1, x=x*2;" a
> well-defined phrase? (intuitively I would assume so and that it would
> go left to right but I'd like to be sure)
I believe that it is well-defined. However, I don't think that the order
of computations is guaranteed. I don't claim to be an expert, though, so
you should wait for a more knowledgable person to answer.
I do know, however, that most well-written code shouldn't care what the
order of computations is, because a maintenance programmer shouldn't be
expected to know. Well-written code should be clear and be evaluated in
one (obvious) way.
> Second question: in one project I maintain, the guy is fond of
> using >> and << for routine division/multiplication. For instance,
> instead of size *= 2, he will write size <<= 1. My question is, is
> there a practical reason for doing this, for routine situations besides
> actual bit twiddling?
Tsk, tsk, tsk. There is no reason for that, except to make the code
unreadable. Tomorrow I might want to multiply by 12 instead of 8; with
multiplication that involves changing one number.
y *= 8; /* In the case that the grommet fails to widge consistently,
multiply by 13 to compensate. Shouldn't be an issue with
the new vendor, though. */
x <<= 2; /* If we're interfacing with DeviceBoy revision F, we need to
shift by 4 to make room for the LCD brightness flags. We
should be compatible before the next version ships. */
See how using an inappropriate operator would make maintenance more
difficult? You should correct your errant colleague.
> I know << is usually faster than *
No you don't.
> , but I always
> assumed any compiler worth a hill of beans would easily know to
> optimize something like "size *= 2" as much as possible anyway?
>
Indeed it will.
> Thanks for helping out, you guys rock =)
>
No problem.
--
Andrew Poelstra <website down>
To reach my email, use <email also down>
New server ETA: 1 days