warning: right shift count >= width of type

Dale Johannesen dalej@apple.com
Mon Nov 29 17:12:00 GMT 2004


On Nov 29, 2004, at 8:18 AM, Dave Korn wrote:
>   Afternoon all.  Here's something that's piqued my curiosity; it's 
> probably
> owing to some language-lawyerly issue, but it isn't obvious to me.  
> This is
> on gcc-3.3.3, (cygwin variant, but that's probably not relevant):
>
> -------------------------<snip!>-------------------------
> dk@mace /test/shift-test> cat foo.c
>
> unsigned int bar (unsigned int baz)
> {
> unsigned int quux;
>
>         quux = baz >> 32;
>         return quux;
> }
>   Why isn't the shift operation optimised away and replaced with 
> const_int
> 0?

Because that's not what it means.  Shifts by >= word size are undefined 
behavior
and will give different results depending on optimization level and on 
whether
the shift count is constant or variable.  Don't do that.  (If you think 
it ought to be 0,
reflect that most popular CPUs have only 5 bit shift counts, and 
consider what the
code for x >> y would have to look like.)



More information about the Gcc mailing list