128. Arithmetic Edge Cases-II

Question.6

A developer needs to compute the difference between two timestamps. Both are uint32_t. The current time may be less than the start time if the timer has wrapped. Which approach correctly handles this?

Option A:

uint32_t elapsed = current - start;  // Unsigned subtraction

Option B:

int32_t elapsed = (int32_t)(current - start);

Option C:

uint32_t elapsed = (current >= start) ?
   current - start : start - current;

Which is correct for a free-running 32-bit timer that wraps around?

Need Help? Refer to the Quick Guide below

Select Answer