Martin Pool's blog

Bitshifts in C

What do you think this does?

uint32_t a, b;
[....]

b = 32; a = a >> b;

You might think this will reduce a down to zero. But in fact, the C99 specification says that shifting either left or right by more than the width of the type causes undefined behaviour. On gcc on i386, it in fact shifts by b % 32, so in this case a is unchanged.

Archives 2008: Apr Feb 2007: Jul May Feb Jan 2006: Dec Nov Oct Sep Aug Jul Jun Jan 2005: Sep Aug Jul Jun May Apr Mar Feb Jan 2004: Dec Nov Oct Sep Aug Jul Jun May Apr Mar Feb Jan 2003: Dec Nov Oct Sep Aug Jul Jun May