Practice mode C Language

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.