Re: Nit-pick non-UB (was Re: about shifting)



"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.


.



Relevant Pages

  • Nit-pick non-UB (was Re: about shifting)
    ... Charlie Gordon wrote: ... "normally" be the case on regular 2s-complement machines, ... Okay, ...
    (comp.lang.c)
  • Re: Walters 87 blank sporting crickets
    ... monarch up to the place. ... They are restricting onto regular, at present okay, after little ...
    (sci.crypt)
  • Re: WOZ Emerald city will have nothing on standard?
    ... worth nothing more than a regular machine? ... significantly worse than the limited edition machines. ... Stern could sell a lot more Tron machines now that people ...
    (rec.games.pinball)
  • Re: Student and Teacher versions... whats the scoop?
    ... What is the difference between the Student versions and the regular ... The user interface for Word 2007 is markedly different from previous ... I do want both of our machines running ... particularly a country song or a pop song or a folk ...
    (microsoft.public.office.setup)
  • Re: Proud Performer
    ... :>I don't know what set you off, dude, but take a pill. ... A regular ... :>okay. ...
    (rec.photo.digital)