What does the C standard say about the result of right-shifting a negative signed integer, as in -8 >> 1?
Correct answer C. The result is implementation-defined
Explanation
C11 says that if E1 has a signed type and a negative value, the result of E1 >> E2 is implementation-defined. Most compilers perform an arithmetic shift and give -4, but portable code cannot rely on it. Left-shifting a negative value is worse, being undefined behaviour. This is another reason to do bit manipulation on unsigned types.
Answer checked How we write and check questions
Discussion