It is undefined behavior to shift by the bit length of a variable or greater. From the draft C99 standard section 6.5.7
Bitwise shift operators:
[...]If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.
As Pascal says, you need to make a special case or use a wider type.
gcc
in some cases can generate a warning for this see it live:
warning: left shift count >= width of type [enabled by default]