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

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -