Go Down

Topic: millis () rollover (Read 1 time) previous topic - next topic


MarkT

Don't worry subtracting two time values will give the right result if the result variable is a signed integer.
(and the time values aren't too far apart - ie less than 2^31 different).

If the result of subtraction is treated as unsigned then it will be exactly the same bit pattern, but the difference will be
conceptually in the range 0.. 2^32-1  (sometimes you always know your difference will be non-negative so this
doubles the range of differences you can handle).

Unlike subtract when you compare two values it matters if they are signed or unsigned - the two comparisons
behave differently (for the same bit-patterns).
[ I won't respond to messages, use the forum please ]

michinyon

What's the difference between ++fakemillis   and   fakemillis++   ?

CrossRoads

++ (increment) / -- (decrement)
Description
Increment or decrement a variable


Syntax
x++;  // increment x by one and returns the old value of x
++x;  // increment x by one and returns the new value of x

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

PaulS

Quote
What's the difference between ++fakemillis   and   fakemillis++   ?

The difference is in what happens if the variable is used during the pre- or post-increment operation.

To illustrate:

Code: [Select]
char buf[10];
byte index = 0;

buf[0] = '\0';

buf[++index] = 'a';
buf[index++] = 'b';


At the end of this snippet, index will have been incremented twice, to 2. In the first case, index will be incremented, and then the value of index will be used as the array index. So, index will be incremented to 1, and then 'a' will be stored in buf[1].

In the second case, the value of index will be used, and then index will be incremented. So, 'b' will be stored in buf[1], and then index will be incremented to 2.

In this scenario, clearly the post-increment form is the desired form, but there are situations where the pre-increment form is desired (although they are not as common).

Go Up