Consider I have a timer that returns a uint32_t
value (representing a number of ticks), always counts upwards, and wraps to 0 after reaching UINT32_MAX
.
Suppose I need to take an elapsed time from time a
to time b
, and I don't know how high the timer might be initially and whether it will wrap between a
and b
. Both a
and b
are type uint32_t
and get assigned to the timer's return value.
Is it a correct statement that we can take (uint32_t)(b-a)
to get the elapsed time so long as no more than UINT32_MAX ticks have elapsed — and will it be correct even if the timer wrapped once? What is the proof for this?