System.DateTime.Now.Ticks help

Hello, i made a robot, and i want to know in how long he runs through a specific distance.

i use :

startTime = System.DateTime.Now.Ticks;
ticks = (int)(System.DateTime.Now.Ticks - startTime) >> 1;
microSeconds = ticks / TicksPerMicrosecond;

it is the best solution ?
and i want to know what is the : β€œ>>1” ? in the line :

ticks = (int)(System.DateTime.Now.Ticks - startTime) >> 1;


1 is a bit shift of one bit. It is a divide by two.

I think the code is attempting to convert the 100 nanosecond ticks into microseconds, but the code would be wrong since the shift of one bit is a binary divide not a decimal divide.

1 Like

i forgot this :

const int TicksPerMicrosecond = (int)TimeSpan.TicksPerMillisecond / 1000;

and so, if i want the real time value, i have to delete the β€œ>>1” ?

@ Tirmit - You can look at one of several β€œstopwatch” entries in Codeshare. I’ve got a barebones one there.

thanks ! it works very well