Computer Arithmetic

Which expression multiplies an integer x by 10 using shifts and additions instead of a multiply instruction?

  1. A(x << 3) + (x << 1)
  2. B(x << 10)
  3. C(x << 2) + x
Show answer and explanation

Answer: A. (x << 3) + (x << 1)

Shifting left by one bit doubles a value, so x << 3 is 8x and x << 1 is 2x. Since 8x + 2x = 10x, (x << 3) + (x << 1) computes ten times x. Multiplication by a constant can be replaced this way with shifts and additions, and compilers choose whichever is faster. x << 10 multiplies by 2¹⁰ = 1024, and (x << 2) + x gives 4x + x = 5x. Be careful not to confuse the shift amount with the multiplier.

  • A. Correct. 8x + 2x = 10x.
  • B. x << 10 multiplies by 1024.
  • C. (x << 2) + x multiplies by 5.
Practice this topic Back to topic

More Computer Arithmetic questions

  1. Why do typical integer dividers (restoring or non-restoring, producing one quotient bit at a time) tend to be slower than multipliers?
  2. What is the result of arithmetic right-shifting the 8-bit signed integer −8 (1111 1000) by one bit?
  3. In C, what are the results of −7 / 2 and −7 >> 1 (with an arithmetic shift) for int?
  4. In C, what is the result of subtracting 1 from 0 in a 32-bit unsigned integer (unsigned int)?
  5. How is overflow detected when adding two unsigned integers?