Last night, my friend tell me a situation as following shows:
in c/c++ ,we write two lines code like this:
int a = 0;
printf("%d\n", 1 << -1);
printf("%d\n", 1 << (a-1));
It is amazing there are two different results, previous is 0, and the other is -2147483648.
We do not know why, and anyone can give me a help.
any word is welcome.
Having a negative operand on the right hand side of a left-shift operator is undefined behavior in C. You shouldn't be amazed at any results from doing it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With