Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>EDIT: The constraints make the simple approach at the bottom invalid. I am adding the binary search function and the final comparison to detect the greater value:</p> <pre><code>unsigned long greater(unsigned long a, unsigned long b) { unsigned long x = a; unsigned long y = b; unsigned long t = a ^ b; if (t &amp; 0xFFFF0000) { x &gt;&gt;= 16; y &gt;&gt;= 16; t &gt;&gt;= 16; } if (t &amp; 0xFF00) { x &gt;&gt;= 8; y &gt;&gt;= 8; t &gt;&gt;= 8; } if (t &amp; 0xf0) { x &gt;&gt;= 4; y &gt;&gt;= 4; t &gt;&gt;= 4; } if ( t &amp; 0xc) { x &gt;&gt;= 2; y &gt;&gt;= 2; t &gt;&gt;= 2; } if ( t &amp; 0x2) { x &gt;&gt;= 1; y &gt;&gt;= 1; t &gt;&gt;= 1; } return (x &amp; 1) ? a : b; } </code></pre> <p>The idea is to start off with the most significant half of the word we are interested in and see if there are any set bits in there. If there are, then we don't need the least significant half, so we shift the unwanted bits away. If not, we do nothing (the half is zero anyway, so it won't get in the way). Since we cannot keep track of the shifted amount (it would require addition), we also shift the original values so that we can do the final <code>and</code> to determine the larger number. We repeat this process with half the size of the previous mask until we collapse the interesting bits into bit position 0.</p> <p>I didn't add the equal case in here on purpose.</p> <hr> <p>Old answer:</p> <p>The simplest method is probably the best for a homework. Once you've got the mismatching bit value, you start off with another mask at 0x80000000 (or whatever suitable max bit position for your word size), and keep right shifting this until you hit a bit that is set in your mismatch value. If your right shift ends up with 0, then the mismatch value is 0.</p> <p>I assume you already know the final step required to determine the larger number.</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