gcc bitwise Leftshift (<<) strange behavior. Here is my code:
#include
#include
void foo(in
Shifting by a value that is equal or greater than the width of the promoted type of the left operand is undefined behaviour, so you must specifically test for and avoid this. In addition, a left-shift of a signed type that results in overflow is also undefined behaviour, so you need to also avoid a shift of 31, too:
printf("1<<(32-n):%d\n", (n > 1 && n < 33) ? 1 << (32-n) : 0);
This particular expression uses 0 for the cases that are otherwise undefined, but you can handle those differently if you need to.