c - Tick time calculation with signed values -


how can rid of potential overflow check if use signed integers?

unsigned long ticks1 = gettickcount(); if (semaphoretake(_sema, to)) // taken {     unsigned long ticks2 = gettickcount();     // take care of potential overflow     unsigned long elapsed = ticks2 > ticks1 ? (ticks2 - ticks1) : (ticks1 - ticks2);     // return rest time     return elapsed < ? - elapsed : 0; } else // timed out     return 0; 

no need overflow check. should ticks2 < ticks1, unsigned math subtraction defined in c result mathematically same elapsed = (ulong_max + 1) + ticks2 - ticks1. not want ticks1 - ticks2.

// unsigned long elapsed = ticks2 > ticks1 ? (ticks2 - ticks1):(ticks1 - ticks2); unsigned long elapsed = ticks2 - ticks1; 

Comments

Popular posts from this blog

c# - SelectList with Dictionary, add values to the Dictionary after it's assigned to SelectList -

how can i manage url using .htaccess in php? -

ios - I get the error Property '...' not found on object of type '...' -