Re: Nit-pick non-UB (was Re: about shifting)
- From: "Charlie Gordon" <news@xxxxxxxxxxx>
- Date: Fri, 21 Sep 2007 00:58:23 +0200
"Kenneth Brody" <kenbrody@xxxxxxxxxxx> a écrit dans le message de news:
46F2AEE1.A4F7C2B8@xxxxxxxxxxxxxx
Charlie Gordon wrote:
[... bit-shifting negative numbers ...]
However, if you expected x <<= 1 to be equivalent to x += x, as would
"normally" be the case on regular 2s-complement machines, you might
as well write the latter.
Okay, nit-pick time related to UB.
Why doesn't the statement:
x += x;
violate 6.5p2:
Between the previous and next sequence point an object shall
have its stored value modified at most once by the evaluation
of an expression. Furthermore, the prior value shall be read
only to determine the value to be stored.
Yes, I see that footnote 71 says that "i = i + 1" is allowed by the
paragraph, but why is the "x" on the right of "+=" not violating the
"shall be read only to determine the value to be stored"? How is
this different from "y = x + x++;" in the use of "x"?
Obviously, something like "x += x;" must be allowed, but what is it
about 6.5p2 that allows it?
x is modified only once, and its value is read only to determine the value
to be stored, once or twice depending on quality of implementation or
presence of volatile qualifier on x's definition.
Change x <<= N into x *= 1 << N to get rid of the problem with negative x,
as long as the multiplication does not overflow. Also note that *all*
current architectures use two's complement representation for integers, and
implement left shifting on negative numbers consistently.
--
Chqrlie.
.
- References:
- about shifting
- From: lak
- Re: about shifting
- From: Richard Bos
- Re: about shifting
- From: Charlie Gordon
- Nit-pick non-UB (was Re: about shifting)
- From: Kenneth Brody
- about shifting
- Prev by Date: Re: how can I return nothing?
- Next by Date: Re: Request for help
- Previous by thread: Re: Nit-pick non-UB (was Re: about shifting)
- Next by thread: Re: Nit-pick non-UB (was Re: about shifting)
- Index(es):
Relevant Pages
|