Weird behavior when dividing hex numbers
<p>I'm struggling with a really weird situation that I can't explain.<br>
Basically I don't understand why results of modulus or division between two hex numbers are wrong (I'm using <code>C</code>).  </p>

<p>I have a variable <code>time</code> which is a <code>32bit unsigned int</code> and it's initialized to 0 and then other two <code>32bit unsigned ints</code>, <code>div</code> and <code>res</code>, both initialized to 0 too.</p>

<pre><code>if (time &gt; 0x1388) {
    div = time / 0x1388;
    res = time - (0x1388 * div);
}
</code></pre>

<p>This chunk of code should act as the <strong>modulus</strong> operator between <code>time</code> and <code>0x1388</code>. <code>Time</code> is a variable which gets incremented all around my program and when it's value is greater than <code>0x1388</code>, I need its remainder to do other calculations.  </p>

<p>This is an example of calculated value that are wrong... Let's suppose <code>time</code> is <code>0x1770</code>, which is greater than <code>0x1388</code>. The division between <code>0x1770</code> and <code>0x1388</code> should be <code>0x1</code> (<code>div</code>) with a reminder of <code>0x3E8</code> (<code>res</code>).
The problem is that after executing this code, <code>div</code> is always <code>0x0FFFFF</code> instead of <code>0x1</code> and <code>res</code> gets a really weird value.  </p>

<p>I already tried to use the modulus but the result is the same. Can someone explain me what's going on?<br>
Maybe is it related to the fact that I'm using <code>unsigned ints</code>? If so, can you please tell me what operations I need to be careful of when using <code>unsigned ints</code>?  </p>

<p><strong>EDIT:</strong> Trying to add some more code...<br>
<strong>file1.c</strong></p>

<pre><code>unsigned int res = 0, div = 0, time = 0; // global variables
unsigned int valueChecker() {
    if (time &gt; 0x1388) {
        div = time / 0x1388;
        res = time - (0x1388 * div);
    }
    return res;
}
</code></pre>

<p>This function is called periodically after a specific amount of time  </p>

<p><strong>fileB.c</strong></p>

<pre><code>extern unsigned int time;

void calculate() {
    // do some stuff
    time += getHEXTime(); // this function has been supplied and return time as an HEX number
    // do other stuff
}
</code></pre>

<p>This function is called by the main several times, between other calculation (which doesn't involve <code>time</code>, <code>div</code> or <code>res</code>).</p>

<p>I'd be so grateful if someone can help me!  </p>