4 years, 2 months ago.

Max value of timer

Hello,

on this link

https://docs.mbed.com/docs/mbed-os-api-reference/en/latest/APIs/tasks/Timer/

is mentioned, that max value of timer is 2^31-1 microseconds. There is also information that timer uses 32-bit int. Now I'm a little bit confused. Why is it 2^31 - 1, when 32 bits are capable of encode 2^32 values? I think max value should be 2^32 - 1 (counting from zero).

Thanks

2 Answers

4 years, 2 months ago.

Timers return signed values, which is why you lose 1-bit to the sign bit. The reason why it is signed is that it makes it easier to subtract different timer values. While not 100% sure, I believe if you store them in a 32-bit unsigned integer you will get 2^32 range. Tickers however are definitely limited to 2^31 seconds in the future, because they also use signed calculations internally. This is done because they could have occurred in the past (for example if you set up a ticker every 1ms, but then you block with another interrupt for 5ms. Then there are multiple ticker occurrences that happened in the past which need to be handled immediately).

Accepted Answer
4 years, 2 months ago.

The "32-bit int" is not "32-bit unsigned int" but "32-bit signed int".
Please refer to https://developer.mbed.org/handbook/C-Data-Types .