Re: Negative values for shift operators.



<ritesh.noronha@xxxxxxxxx> wrote in message
news:1123440810.401075.237490@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> K&R states that the right operand must be non-negative.
>
> "The shift operators << and >> perform left and right shifts of their
> left operand by the number of bit positions given by the right operand,
> which must be non-negative"
>
> unsigned int n = 10;
> n <<= -2;
>
> MSVC7 accepts this above code as valid and compiles it.
>
> Should this be allowed, it seems like it could lead to subtle bugs?

Just because MSVC7 did that, doesn't matter everyone else would. It is
unreasonable to think that if this particular compiler does the trick
another does too. You must never use shifts by negative amount/number of
places or you're looking for a trouble. The K&R statment is still valid,
that's what the current standard states as well. The best practice is to
avoid implementation specific things and things that may cause undefined
behaviour. If there's a doubt or discrepancy among the compilers or between
a compiler and the standard, don't do that damned thing. Just because in
many places for a pointer to data or function you see DWORD or something of
that sort, doesn't meen it's good.

Alex


.



Relevant Pages

  • Re: [C, C++] "(a=b) = c;" Illegal or Undefined?
    ... What I am saying is that a compiler might look ... order of evaluation of the left and right operand is unspecified. ... occurs somewhere during the previous and next sequence point. ... assignment and 4 after assignment. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Multiple Assignment Evaluation Debate
    ... have no right to expect anything in particular, because a compiler ... "int", it will still have 3 in it later. ... An assignment operator stores a value in the object ... designated by the left operand. ...
    (comp.lang.c)
  • Re: Ambiguity in semantics of assignments?
    ... > on the compiler. ... the left operand after the assignment, ... The type of an assignment expression is the type of the left ...
    (comp.lang.c)
  • Re: Microsoft patents isnot operator
    ... the system comprising: a compiler for ... > receiving source code and generating executable code from the ... > second operand, the expression evaluating to true when the ... We need to see the full patent I think. ...
    (sci.physics)
  • Re: Microsoft patents isnot operator
    ... the system comprising: a compiler for ... > receiving source code and generating executable code from the ... > second operand, the expression evaluating to true when the ... We need to see the full patent I think. ...
    (comp.os.linux.development.apps)