Re: The result of 1 << 32



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?

No, the result of undefined behaviour is not required to be consistent. If undefined behaviour had to be consistent then that would effectively mean having to detect buffer overruns, otherwise how could it provide consistent behaviour on the undefined behaviour caused by writing beyond the end of a buffer?
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
.




Relevant Pages

  • Re: The result of 1 << 32
    ... the result of undefined behaviour is not required to be consistent. ... >> mean having to detect buffer overruns, ... inputs lead to a buffer overrun, that in a majority of cases the ...
    (comp.lang.c)
  • Re: The result of 1 << 32
    ... Flash Gordon wrote: ... the result of undefined behaviour is not required to be consistent. ... > mean having to detect buffer overruns, ...
    (comp.lang.c)
  • Re: The result of 1 << 32
    ... the result of undefined behaviour is not required to be consistent. ... >> mean having to detect buffer overruns, ... (or compilers) ...
    (comp.lang.c)
  • Re: how strcpy works
    ... It may /seem/ consistent, but that is ... When you rely on undefined behaviour, you are inviting the forces of ... John Dallman, jgd@xxxxxxxxx, HTML mail is treated as probable spam. ... have an appropriate newsgroups line in your header for your mail to be seen, ...
    (comp.lang.c.moderated)
  • Re: gdb not catching out-of-bounds pointer
    ... statement about 'undefined behaviour' because of something. ... about legitimacy of address calculations, a statement about what is it ... You know the standard as well as I do. ... The compiler writer may take ...
    (comp.unix.programmer)