ITPEC FE Morning October 2023 Question 2

n is a binary integer represented in two’s complement. Which of the following operations get the value 7 × n using only bit shifting and an addition or subtraction operation?

Explanation

Shift 3 bits left, then subtract n — a left shift by k bits multiplies by 2k, and 7 is one less than 23.

Write 7 as 8 − 1 = 23 − 1, so 7 × n = 8 × n − n: shift n left 3 bits to get 8n, then subtract n. In two's complement a left shift multiplies by 2 for negative values too (barring overflow), so the method holds for any sign.

Answer (d)

Why not others:

  • •(a) — 4n + n = 5n: a 2-bit shift gives only 4n
  • •(b) — 4n − n = 3n
  • •(c) — 8n + n = 9n: adding n overshoots by 2n

Key rule: To multiply by a constant with shifts, write it as a sum or difference of powers of two; a constant just below a power of two, such as 7, 15 or 31, is that power minus 1.

Basic Theory · AI-written, may contain errors

This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.