Difficulty: Easy
Correct Answer: 2 and 4
Explanation:
Introduction / Context:
This item tests knowledge of shift operators and clarifies that ^
is bitwise XOR, not exponentiation. You must determine which pair among the given expressions yields the same integer result.
Given Data / Assumptions:
int
arithmetic.>>
is arithmetic right shift; <<
is left shift; >>>
is logical right shift; ^
is XOR.
Concept / Approach:
Evaluate each expression explicitly. Compare the results to find equal pairs. Note that right-shifting 128 by 2 places (logical) gives 32. Composing shifts on 8 as shown produces another constant you can compute directly.
Step-by-Step Solution:
Verification / Alternative check:
Quick mental binary: 128 is 1 at bit 7; shifting right 2 → bit 5 set → 32. For expression 2, 8 (bit 3) >> 2 → bit 1 (value 2); << 4 → bit 5 (value 32).
Why Other Options Are Wrong:
Common Pitfalls:
Confusing ^
with power and overlooking that logical vs arithmetic right shifts differ only for negative operands.
Final Answer:
2 and 4
Discussion & Comments