You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fixed comparisons with (wrong) unsigned-to-signed integer promotions
The following snippet demonstrates the problem:
uint32_t millis0 = 0x5FFF0;
uint32_t millis1 = 0x60010;
uint16_t t0 = millis0;
Serial.println((uint16_t)millis1-t0); // prints -65504 WRONG
Serial.println((uint16_t)(millis1-t0)); // prints 32 OK
the subtraction promotes each of the operands to signed integers so
we must make sure that the cast is made after the operation.
To further simplify this logic and avoid this kind of subtle traps
the operation has been moved from the condition into a variable of
the desired size:
uint16_t d = millis1 - t0;
Serial.println(d); // prints 32 as well
Fix#41
0 commit comments