Practice · 1 of 1
Saturating Arithmetic
CHALLENGE
Difficulty: intermediate+25 XP
Implement overflow-proof integer ops. The boilerplate declares:
``c
/* a + b saturated into [INT_MIN, INT_MAX] */
int sat_add(int a, int b);
/* a - b saturated */
int sat_sub(int a, int b);
/* a * b saturated (compute in long long) */
int sat_mul(int a, int b);
/* x << k as unsigned; 0 if k is out of range [0, 31] */
unsigned safe_shl(unsigned x, unsigned k);
``
No signed overflow may occur in your implementation — that is the whole
exercise.
Press Submit to check your solution.
Back to lesson: Practice: Boundary Gym