Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you don't have a 64-bit type, you can do it like the following:</p> <pre><code>uint32_t higher, lower; // your input lower /= 1000; lower += (higher % 1000) * 4294967L; // approximate 2^32 / 1000 higher /= 1000; </code></pre> <p>If the result fitted in <code>lower</code> itself, <code>higher</code> should be <code>0</code>.</p> <p>Just note that as @Mikhail pointed out, this solution is approximate, and has an error of <code>0.296 * higher + 2</code> ms (unless I'm missing something).</p> <hr> <p>If you <em>really</em> 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:</p> <pre><code>uint32_t higher, lower; // your input // simpler without a helper variable if (lower % 1000 &gt;= 500) { lower /= 1000; ++lower; } else lower /= 1000; lower += round((higher % 1000) * 4294967.296); // 2^32 / 1000 higher /= 1000; </code></pre> <p>You'll need to <code>include &lt;cmath&gt;</code> for <code>round()</code>.</p> <p>As a note, @Mikhail's solution in this case is probably better and <em>may</em> be faster. Though it's too complex for me.</p> <hr> <p>If you have a 64-bit type, you can convert the split value to it:</p> <pre><code>uint64_t whole_number = higher; whole_number &lt;&lt;= 32; whole_number |= lower; </code></pre> <p>And then you can use <code>whole_number</code> as usual.</p> <hr> <p>Note that if you only need a difference, it will be faster to subtract the values before actually dividing.</p> <p>Assuming that you know which value is bigger:</p> <pre><code>uint32_t higher1, lower1; // smaller value uint32_t higher2, lower2; // bigger value uint32_t del_high = higher2 - higher1; uint32_t del_low = lower2 - lower1; if (lower2 &lt; lower1) --del_high; </code></pre> <p>And now you can convert the result like explained before. Or with a bit luck, <code>del_high</code> will be <code>0</code> (if the difference is smaller than 2^32 μs), and you will have the result in <code>del_low</code> (in μs).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload