<p>I have some code that looks like:</p>

<pre><code>template&lt;unsigned int A, unsigned int B&gt;
int foo() {
  int v = 1;
  const int x = A - B;
  if (x &gt; 0) {
    v = v &lt;&lt; x;
  }
  bar(v);
}
</code></pre>

<p>gcc will complain about x being negative for certain instantiations of A, B; however,  I do perform a check to make sure it is non-negative.  What's the best way around this?  I know I can cast <code>x</code> to be <code>unsigned int</code> but that will cause warnings about <code>x</code> being larger than the width of <code>v</code> (since it is casting a negative number to be positive).  I know there is a work-around that involves creating a new templatized <code>shift</code> function, but I'd like to avoid that if possible.</p>

Would this work?
```
```
const short unsigned int x = A - B;
```
```
It's cutting off a lot more bits than need to be cut off, but if your values of A - B are small enough...