In compiler optimization, operator strength reduction uses mathematical…
2016
In compiler optimization, operator strength reduction uses mathematical identities to replace slow math operations with faster operations. Which of the following code replacements is an illustration of operator strength reduction ?
- A.
Replace P + P by 2 * P or Replace 3 + 4 by 7.
- B.
Replace P * 32 by P < < 5
- C.
Replace P * 0 by 0
- D.
Replace (P < <4) – P by P * 15
Attempted by 74 students.
Show answer & explanation
Correct answer: B
Answer: Replace P * 32 by P << 5
Why this is operator strength reduction:
Operator strength reduction replaces an expensive operation with a cheaper, mathematically equivalent one (for example, replacing multiplication or division by additions, subtractions, or bit shifts).
Since 32 = 2^5, multiplying by 32 is equivalent to shifting left by 5 bits for integer values. Bit shifts are generally faster than general-purpose integer multiplication, so replacing P * 32 with P << 5 is a classic strength-reduction move.
Why the other replacements are not operator strength reduction:
Replacing P + P by 2 * P turns an addition into a multiplication. Multiplication is usually not cheaper than addition, so this is not strength reduction; it may even be counterproductive.
Replacing 3 + 4 by 7 is constant folding (evaluating compile-time constants), a different optimization that does not illustrate strength reduction.
Replacing P * 0 by 0 is algebraic simplification/constant propagation, not strength reduction.
Replacing (P << 4) - P by P * 15 converts shift and subtraction (typically cheaper) into multiplication (typically more expensive). This is the reverse of strength reduction.
Practical note: ensure the data type and semantics allow the transformation (for example, confirm integer arithmetic and consider signed vs unsigned behavior and potential overflow) before applying shift-based replacements.