Strangeness with shifts



 DEVELOP > c-Plus-Plus > Strangeness with shifts

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Glen Able"
Date: 23 Nov 2003 05:38:40 AM
Object: Strangeness with shifts
The behaviour of << and >> arithmetic operators (under VC++6, x86) seems
rather odd to me.
Shifts at runtime seem to only use the bottom 5 bits of the shift amount (an
x86 issue I suppose), but the compile time evaluation will do as you'd
expect (so i << 32 is 0, rather than i).
Also compile time shifts do some weird clamping where, e.g.
-99 >> 20 is -1
and -99 >> -1 is -1
Does any of this seem acceptable behaviour (particularly the fact that a
simple arithmetic operation can have different results and compile and
runtime) ?
thanks.
.

User: "Florian Weimer"

Title: Re: Strangeness with shifts 23 Nov 2003 07:24:12 AM
"Glen Able" <removextoreplysmexcklxers@hoxtmxail.com> writes:

Does any of this seem acceptable behaviour (particularly the fact that a
simple arithmetic operation can have different results and compile and
runtime) ?

According to the standard, behavior in these cases is undefinded, so
the implementation is correct.
.

User: "Jack Klein"

Title: Re: Strangeness with shifts 23 Nov 2003 08:56:14 PM
On Sun, 23 Nov 2003 11:38:40 -0000, "Glen Able"
<removextoreplysmexcklxers@hoxtmxail.com> wrote in comp.lang.c++:

The behaviour of << and >> arithmetic operators (under VC++6, x86) seems
rather odd to me.

Shifts at runtime seem to only use the bottom 5 bits of the shift amount (an
x86 issue I suppose), but the compile time evaluation will do as you'd
expect (so i << 32 is 0, rather than i).

Also compile time shifts do some weird clamping where, e.g.
-99 >> 20 is -1
and -99 >> -1 is -1

Does any of this seem acceptable behaviour (particularly the fact that a
simple arithmetic operation can have different results and compile and
runtime) ?


thanks.

From the ISO C++ standard:
=====
[expr.shift] 5.8 Shift operators
1 The shift operators << and >> group left to right.
[snip grammar]
The operands shall be of integral or enumeration type and integral
promotions are performed. The type of the result is that of the
promoted left operand. The behavior is undefined if the right operand
is negative, or greater than or equal to the length in bits of the
promoted left operand.
=====
So when you invoke undefined behavior with a right operand of greater
than 31 (for Visual C++), the C++ language no longer knows nor cares
what happens. When you generate undefined behavior, you most
certainly have different results even with "simple arithmetic
operation".
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER