Quant Memo
Coding/●●●●●

Saturating 32-bit addition

In signal processing and fixed-point trading math you often want saturating addition: a + b, but if the true sum exceeds the 32-bit range it clamps to INT_MAX or INT_MIN rather than wrapping around to a garbage value.

Assume a and b are each already valid 32-bit signed integers (in [231, 2311][-2^{31},\ 2^{31}-1]).

Write a saturating add that never computes an intermediate outside the 32-bit range, so it ports to C or Rust untouched.

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions