Re: Negative values for shift operators.
- From: "Alexei A. Frounze" <alexfru@xxxxxxx>
- Date: Mon, 8 Aug 2005 02:25:35 +0400
<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
.
- References:
- Negative values for shift operators.
- From: ritesh . noronha
- Negative values for shift operators.
- Prev by Date: Re: Negative values for shift operators.
- Next by Date: Newsreaders - and degrees of evil (Was: Re: switching on strings in standard C)
- Previous by thread: Re: Negative values for shift operators.
- Next by thread: Re: Negative values for shift operators.
- Index(es):
Relevant Pages
|