ITPEC FE Morning April 2022 Question 2

For non-negative integer A, which of the following has the same value as (A mod 32) + 64? Here, mod, +, AND, and OR are remainder-after-division, arithmetic addition, bitwise-AND, and bitwise-OR operators, respectively.

Explanation

(A AND 31) OR 64 — mod by a power of two keeps the low bits, and adding a bit that is not already set is the same as ORing it in.

  • •32 is 25, so A mod 32 is the lowest five bits of A. Masking with 31 (11111) keeps exactly those bits: A mod 32 = A AND 31.
  • •That value is at most 31, so bit 6 (value 64, 1000000) is always 0 in it. Adding 64 therefore produces no carry and simply sets bit 6: x + 64 = x OR 64.

Answer (a)

Why not others:

  • •(b) — AND 32 keeps only bit 5 of A, and OR 32 then sets bit 5, so the result is always 32
  • •(c) — OR 31 sets the low five bits to 1 instead of keeping them, and AND 64 then clears everything except bit 6, giving 0 or 64
  • •(d) — AND 32 keeps only bit 5 of A, so the result is 0 or 32; the 64 set by OR is cleared again

Key rule: For n = 2k, A mod n = A AND (n − 1), and + becomes OR only when the two operands share no 1 bits.

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.