warning: left shift count >= width of type

Hello,

When compiling this code i get the warning above

void setup() {
unsigned long toShift=(1<<16);
}

void loop() {
;
}

But the width of long is 32 and the shift count here is 16 (?!)

By default, integers are 16-bit (doesn't matter what the target type is on the LHS) so trying to shift 1 left by 16 bits would result in zero.
Use 1L << 16

Pete

The type of "toshift" is irrelevant (it only matters when the = is evaluated). This (1<<16) is an int!

Mark

surprising as this is a compile time integer constant so I don't see why the compiler should bother of the "type" of the entity '1'. I expected it to evaluate it to 2^16 and only then check if this is compatible with LHS type. Good I asked. Many thanks

unsigned long toShift = 1UL << 16;

or

unsigned long toShift = 1;
toShift <<= 16;

Thanks, I coded
toShift=1L<<16
and it compiled w/o warning and was correctly initialized

You might as well just write

unsigned long toShift=0x00010000 ;

It makes sense to use these shifts if you are trying to match a particular bit in a control register, which might vary between implementations, or something, but then you would use a specific symbolic name, anyway.