Re: The result of 1 << 32



In article <dk1j2q$548$1@xxxxxxxxxxxxxxxxxxxxxxx>,
roberson@xxxxxxxxxxxxxxxxxx (Walter Roberson) wrote:

> In article <slrndm762i.o4v.jmabel@xxxxxxxxxxxxx>,
> Jordan Abel <jmabel@xxxxxxxxxx> wrote:
> >On 2005-10-29, mrby <bianying@xxxxxxxxx> wrote:
>
> >> Why 1<<i and 1<<32 get different result? I had thought
> >> both of them would get 0x0.
>
> >A theory: the 1<<32 is calculated at compile time [and the compiler
> >itself is smart enough to do this] while the 1<<i value is
> >calculated at runtime - now, if your processor's LSH instruction
> >only accepts a 5-bit immediate operand.
>
> >According to the standard, shifting by a number greater than or
> >equal to the field width is undefined.
>
> I don't have my reference material here, but I seem to recall that
> constant calculations are supposed to be done "as if" they were
> done at run-time. Possibly that only applied to calculations in
> preprocessor expressions.
>
> If my recollection is correct, then even though the shift behaviour
> is undefined, would it not be required to be consistant?

"Undefined behavior" includes permission to be inconsistent. If the
hardware produces a random number as a result of trying to calculate 1
<< 32, then the compiler can do the same thing at compile-time.

In many implementations, it would be possible to define the behavior of
x << y for arbitrary values of y. If the implementation defines the
behavior, then it would implement it both at compile time and at runtime
consistently with that definition.

If the hardware behaves consistently, but the implementation doesn't
actually _define_ that behavior, then all bets are off. However, this
could be the source of an incredibly hard to find bug. For example, in x
<< y a compiler might figure out that y always has the same value at a
high enough optimisation level. So in the example

int i = 32;
int result = 1 << i;

a compiler might do a shift at runtime when it is not optimising, and do
a shift at compile time when it is optimising. Good luck finding the bug
if that happens.
.



Relevant Pages

  • Re: speed up calculation suggestions
    ... We use Fortran for some or our calculations and are being hampered ... positively the first thing to do is to profile your code. ... "Optimize" should be interpreted to mean enabling the compiler to ... algorithm. ...
    (comp.lang.fortran)
  • Re: GAMESS on 64bit architecture
    ... The machine needs to be fairly cheap, so, ... > What type of compiler did you use? ... least 2 SATA disk drives and RAID-0 them, CC calculations are quite disk ... drives would not be overkill. ...
    (sci.chem)
  • Re: sorting the input
    ... Ben Bacarisse said: ... The compiler does some calculations to determine the type and hence ... have licence to get it wrong. ...
    (comp.lang.c)
  • Re: I wasnt expected that !
    ... The compiler might even be as clever as to simply *not do* some of the ... calculations at all, since it knows the variables don't get used in a ... > versions of the software for occasional programming sessions. ... > means six general math operations per loop, which means about 540,000,000 ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Release Dll Problem - Optimization Issue
    ... If optimising for speed on smaller size processors, ... This limits the compiler in using fast registers for the ... and speeds up access to the variable. ...
    (microsoft.public.pocketpc.developer)