I have big number, time (micro seconds) stored in two 32bit variables.
I need a help, how to change micro seconds time into millisecond, so I can store result of difference in 32bit number.
More details:
I have one time in two 32bit variables. Where one variable have more significant bits and other have less significant bits. This time have microseconds resolution so I want to change it to milliseconds. So how to divide number that is stored in two variables.
If you don’t have a 64-bit type, you can do it like the following:
If the result fitted in
loweritself,highershould be0.Just note that as @Mikhail pointed out, this solution is approximate, and has an error of
0.296 * higher + 2ms (unless I’m missing something).If you really want a better precision and don’t care about efficiency, you can use a bit of floating-point arithmetic in the middle, and round the results correctly. I doubt if it’s worth the effort:
You’ll need to
include <cmath>forround().As a note, @Mikhail’s solution in this case is probably better and may be faster. Though it’s too complex for me.
If you have a 64-bit type, you can convert the split value to it:
And then you can use
whole_numberas usual.Note that if you only need a difference, it will be faster to subtract the values before actually dividing.
Assuming that you know which value is bigger:
And now you can convert the result like explained before. Or with a bit luck,
del_highwill be0(if the difference is smaller than 2^32 μs), and you will have the result indel_low(in μs).