left shift count >= width of type with constant, compile-time expression

Jim Cromie jim.cromie@gmail.com
Tue May 2 13:14:00 GMT 2006



$ more shift-prob.c

#include <stdio.h>
typedef signed long long s64;
typedef unsigned long long u64;
typedef u64 cycle_t;

#define CLKSRC_MASK(bits)       (cycle_t)((1<<bits)-1)
static cycle_t foo = CLKSRC_MASK(2);
static cycle_t bar = CLKSRC_MASK(30);
static cycle_t buz = CLKSRC_MASK(31);
static cycle_t baz = CLKSRC_MASK(32);

int main(int c, char** v)
{
    printf("foo %llx bar %llx buz %0.llx baz %llx\n",
           foo, bar, buz, baz);
}


$ make shift-prob
cc -g    shift-prob.c   -o shift-prob
shift-prob.c:13: warning: integer overflow in expression
shift-prob.c:14: warning: left shift count >= width of type

$ shift-prob
foo 3 bar 3fffffff buz 7fffffff baz ffffffffffffffff

IOW, the 2nd warning predicts the problem - the 4th var shouldnt have 
all FFs
It seems the compiler disregards the unsignedness.
Or, what did I miss ?

$ cc --version
cc (GCC) 4.0.2 20051125 (Red Hat 4.0.2-8)
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


I found this old post,
http://gcc.gnu.org/ml/gcc-help/2005-07/msg00126.html

but the question was about run-time shifting, with variables computing 
the shift value,
and OP used ints, not unsigneds.
The answer was nebulous wrt exactly what was undefined.

tia
jimc



More information about the Gcc-help mailing list