= Solution
For a 32-<bit> <machine word> and $1\leq k\leq31$, use a <left bit shift> followed by a <logical right shift>:
``
lsls r1, r0, #(32-k)
lsrs r1, r1, #(32-k)
``
The left shift discards all but the lowest $k$ <bits> and moves those retained <bits> to the top. The <logical right shift> returns them to their original positions and fills the rest with zero. Hence \b[`r1` contains $a\bmod 2^k$], the remainder identified in (b). For $k=0$ the remainder is zero; for $k=32$ it is $a$, so these endpoints can be handled directly.
On a <reduced instruction set computer>, a <bit mask> such as $2^k-1$ may not fit an instruction's immediate field. Masking may therefore require materializing the constant in another <processor register>, possibly using several instructions or a memory load. Small shift counts are directly encodable, so the two shifts can save code space, a temporary <processor register>, or a load. This is an architecture-dependent advantage, rather than a universal speed claim. The source's compact instruction forms support these immediate shifts, while their <bitwise AND> form takes its other operand from a <processor register>.<Thumb 16-bit instruction set quick reference by Arm>[page 1, Logical and Shift/rotate tables]{ref}
Back to article page