KatInterview

32-Bit Integer Product via Shift-and-Add

Math & Bit ManipulationAuto-graded · 28 test cases
MediumAuto-gradedMath & Bit Manipulationbit-manipulationmathoverflow

Last updated

Tests mastery of bitwise arithmetic, sign handling, two's-complement overflow semantics, and O(log n) shift-and-add multiplication without using * or /.

32-Bit Integer Product via Shift-and-Add

You must compute the product of two 32-bit signed integers without using the * or / operators at any point in your implementation.

Your function receives two integers a and b (each guaranteed to fit in a 32-bit signed type) and a boolean flag clamp. It returns their product interpreted under 32-bit signed arithmetic, with behavior controlled by clamp.

Core algorithm

Decompose the magnitude of b into its binary representation. The product |a| × |b| equals the sum of |a| × 2ⁱ for every bit position i where b has a 1. Start with an accumulator at 0 and a running term equal to |a|. On each iteration, if the current low bit of |b| is set, add the running term to the accumulator; then shift |b| right by one and double the running term (via self-addition). This is equivalent to the classical Russian peasant algorithm and runs in O(log |b|) additions.

Determine the sign of the result separately: the product is negative exactly when one input is negative and the other is not. Multiply magnitudes, then apply the sign.

Two modes

Unchecked mode (clamp=False)

Simulate 32-bit two's-complement wraparound. Reduce every intermediate value modulo 2³² using a bitmask. After the magnitude loop, negate if necessary (also under the mask), then reinterpret the 32-bit pattern as a signed integer. Do not use any 64-bit integer type.

Saturating mode (clamp=True)

Instead of wrapping, pin the result to the nearest representable boundary when the true product would overflow:

  • Return INT_MAX (2³¹ − 1) if the result would exceed it.
  • Return INT_MIN (−2³¹) if the result would fall below it.

To detect overflow without ever forming an oversized value, compare before adding: if addend > limit − result, the addition would overflow, so saturate immediately. Use an appropriate limit based on the sign of the final result:

  • Positive results: limit = INT_MAX = 2³¹ − 1
  • Negative results: limit = 2³¹ (because |INT_MIN| = 2³¹, one more than INT_MAX)

When doubling the running term, check whether it would exceed limit before doing so. If it would, assign it a sentinel value of limit + 1; any future use of this sentinel correctly triggers saturation. Do not use any 64-bit integer type.

Constraints

  • -2³¹ ≤ a, b ≤ 2³¹ − 1 (standard 32-bit signed range)
  • clamp is a plain Python bool
  • Do not use * or / anywhere in your implementation
  • Do not use 64-bit integer types (no numpy.int64, no intermediate Python ints wider than 32 bits for the saturating path — rely on the pre-addition check instead)
  • Your solution must run in O(log |b|) time, not O(|b|)
  • 0 × x and x × 0 must return 0 for all x, including negative values
  • INT_MIN × −1 wraps to INT_MIN in unchecked mode and saturates to INT_MAX in saturating mode

Examples

Example 1

Input: multiply_32(0, 12345, False)

Output: 0

Example 2

Input: multiply_32(7, 6, False)

Output: 42

Example 3

Input: multiply_32(-7, 6, False)

Output: -42

Example 4

Input: multiply_32(-7, -6, False)

Output: 42

Example 5

Input: multiply_32(2147483647, 2, False)

Output: -2

Example 6

Input: multiply_32(-2147483648, -1, False)

Output: -2147483648

Example 7

Input: multiply_32(2147483647, 2, True)

Output: 2147483647

Example 8

Input: multiply_32(-2147483648, -1, True)

Output: 2147483647

Hints

Hint 1

Separate sign from magnitude at the start. Determine whether the result is negative by checking if exactly one input is negative, then work entirely with non-negative magnitudes until the very end.

Hint 2

In unchecked mode, apply a 32-bit mask `(1 << 32) - 1` after every addition and every left-shift to keep values from growing beyond 32 bits. Negate the final magnitude under the same mask before reinterpreting it as signed.

Hint 3

In saturating mode, set `limit = INT_MAX` for a positive result and `limit = 2³¹` for a negative one. Before each addition, check `addend > limit - result` to detect overflow without actually computing the sum. When doubling the running term, apply the same style of pre-check and assign a sentinel if it would exceed the limit.