Difficulty: Easy
Correct Answer: No — use left shift (x << k) to multiply by 2^k; OR does not perform scaling
Explanation:
Introduction / Context:
This question ensures you can distinguish operations that change numeric magnitude (shifts and multiplication) from those that merely combine bits (OR). While shifts are algebraically tied to powers of two, OR simply sets bits and does not guarantee any relationship to arithmetic scaling.
Given Data / Assumptions:
Concept / Approach:
Left shift by k (x << k) is equivalent to x * 2^k modulo the machine width, provided no overflow concerns beyond the type width. In contrast, OR with a value does not scale x; it only forces certain bits to 1. The result of an OR depends on the bit pattern of x and the mask, not on a constant scaling rule. Therefore, OR cannot replace shift for multiplication by powers of 2.
Step-by-Step Solution:
Verification / Alternative check:
Repeat with several values; OR never consistently equals multiplication by 2^k, while left shift does (modulo width/overflow behavior).
Why Other Options Are Wrong:
Common Pitfalls:
Assuming bit-setting implies numeric scaling; forgetting that shift changes position (weight) of bits, whereas OR only merges patterns.
Final Answer:
No — use left shift (x << k) to multiply by 2^k; OR does not perform scaling.
Discussion & Comments