networking · beginner · ~15 min
Compute the tv_usec remainder.
The companion to the seconds split: compute the sub-second remainder of a millisecond duration, expressed in microseconds, for the tv_usec field of a struct timeval.
Implement long ms_to_usec(long ms) that returns the leftover microseconds — the part of ms below a whole second, converted to microseconds.
One long ms — a duration in milliseconds.
Returns (ms % 1000) * 1000, the sub-second remainder in microseconds.
ms_to_usec(1500) -> 500000
ms_to_usec(3000) -> 0
ms_to_usec(250) -> 250000
One long: a duration in milliseconds.
The sub-second remainder of ms in microseconds.
Take ms modulo 1000, then multiply by 1000 to convert to microseconds.
long ms_to_usec(long ms) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.