Skip to main content

Practice ยท 1 of 1

Saturating Arithmetic

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.

Difficulty: intermediate

Back to lesson: Practice: Boundary Gym