Difficulty: Easy
Correct Answer: Correct — it is implementation-defined and may differ by compiler or CPU
Explanation:
Introduction / Context:
This question probes your knowledge of how the right-shift operator works on signed integers. Unlike unsigned right shifts, which are unambiguous, right shifts of negative signed integers can differ by platform because of how the sign bit is treated (arithmetic vs logical shift).
Given Data / Assumptions:
Concept / Approach:
For unsigned types, right shift inserts zeros on the left (logical shift). For signed types, the C and C++ standards permit implementation-defined behavior: compilers may perform an arithmetic shift (propagating the sign bit, keeping the value close to division by 2 rounding toward negative infinity) or a logical shift (inserting zeros). Most two’s-complement compilers choose arithmetic shift, but the language does not require it. Thus, results may vary across compilers and systems.
Step-by-Step Solution:
Verification / Alternative check:
Consult your compiler’s documentation or test with a small program to see whether -8 >> 1 yields -4 (arithmetic) or something else (logical).
Why Other Options Are Wrong:
Common Pitfalls:
Assuming all compilers use arithmetic shift; writing portable code that depends on a specific signed shift behavior.
Final Answer:
Correct — it is implementation-defined and may differ by compiler or CPU.
Discussion & Comments