Negative values for shift operators.

R

ritesh.noronha

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?

rcn
 
E

Eric Sosman

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?

The behavior of the shift is undefined, meaning that
the C Standard makes no guarantees about what might happen
if the program attempts to evaluate the offending expression.
"Anything can happen" includes as a special case right-shifting
`n' by two bit positions -- but it also includes left-shifting
by thirty positions, not shifting at all, or causing your CPU
to overheat and melt.

So, yes: it could lead to bugs, subtle and unsubtle, just
like dividing by zero or writing to the [105] position of a
three-element array.

However, the Standard does not require the compiler to
catch every misteak in the program. Some mistakes must be
caught (in the sense that the compiler is required to produce
a diagnostic message), but not all. Some compilers will catch
more mistakes than they are required to; some will even issue
warnings about valid but "suspicious" code. It appears that
the compiler you are using doesn't catch this particular mistake;
it's not among the "must catch" mistakes, so that's permissible.
Perhaps the compiler supports different "warning levels" and
would catch this mistake if you increased its "sensitivity;"
check the documentation for the compiler to see if there's a
way to get it to be pickier.
 
A

Alexei A. Frounze

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top