Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you really need to good performing comparation of integers of arbitrary sizes I recommend you look at <a href="http://gmplib.org/" rel="nofollow">The GNU Multiple Precision Arithmetic Library</a>. That requires you to use it's special mpz_t type (which has the length included). Then you can use the function <code>int mpz_cmp(mpz_t op1, mpz_t op2)</code>. Deciding on your own representation of big integers and implement it in way that is both fairly portable and efficient is not trivial.</p> <p>If, on the other hand, you just need the standard integer sizes you mention I think your implementation is fine. But for even better portability you shouldn't make assumptions on the various integer sizes:</p> <pre><code>#include &lt;stdint.h&gt; int intcmp(const void *a, const void *b, size_t size) { switch (size) { case 1: return (*(int8_t*)a &gt; *(int8_t*)b) - (*(int8_t*)a &lt; *(int8_t*)b) case 2: return (*(int16_t*)a &gt; *(int16_t*)b) - (*(int16_t*)a &lt; *(int16_t*)b) case 4: return (*(int32_t*)a &gt; *(int32_t*)b) - (*(int32_t*)a &lt; *(int32_t*)b) case 8: return (*(int64_t*)a &gt; *(int64_t*)b) - (*(int64_t*)a &lt; *(int64_t*)b) } assert(0); return 0; } </code></pre> <p>Perhaps you'll find it better to create a separate function for each length you need instead of using the same for all? And finally, if efficiency is important, it is often less efficient to do arithmetics with <strong>char</strong> or <strong>short</strong> than with <strong>int</strong>. So try to avoid the cases where you need to call this function with char or short and use int instead.</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