title: C-Style Arithmetic in Cython on unsigned int
body: <p>Is there an easy way to make left shift, and subtraction work as they would in C on unsigned integers with Cython?</p>
  
  <p>For instance:</p>
  
  <pre><code>def left_shift(unsigned int x, unsigned int shift):
    return x &lt;&lt; shift
  
  def main():
    print left_shift(0xffffffff, 4)
    print left_shift(0xffffffff, 8)
    print left_shift(0xffffffff, 12)
  </code></pre>
  
  <p>I would expect this to print the decimal equivalent of </p>
  
  <pre><code>0xfffffff0
  0xffffff00
  0xfffff000
  </code></pre>
  
  <p>and that is actually what I get.</p>
  
  <pre><code>4294967280
  4294967040
  4294963200
  </code></pre>
  
  <p>However, if I try to do something more elaborate such as use one of Jenkins' hash functions on a large input, this is what I get:</p>
  
  <pre><code>def hash_fcn1(unsigned int key):
    key = (key ^ 0xdeadbeef) + (key &lt;&lt; 4)
    key = key ^ (key &gt;&gt; 10)
    key = key + (key &lt;&lt; 7)
    key = key ^ (key &gt;&gt; 13)
    return key
  
  hash_fcn1(0xffffffff)
  
  File "./hash_fcn_test.py", line 94, in &lt;module&gt;
      main()
    File "./hash_fcn_test.py", line 60, in main
      print hash_fcn1(0xffffffff)
    File "hash_fcns.pyx", line 6, in hash_fcns.hash_fcn1 (/home/medusa/.pyxbld/temp.linux-x86_64-2.7/pyrex/hash_fcns.c:854)
      key = (key ^ 0xdeadbeef) + (key &lt;&lt; 4)
  **OverflowError: value too large to convert to unsigned int**
  </code></pre>
  
  <p>Similar issues arise when the value of a computation would result in a negative number.  Is there any way around these issues?  I would like the computation to behave just as it does in C.  Is that too much to ask?  I've scoured the web, and it appears that the common practice is just to <strong><em>bitwise and (&amp;)</em></strong> each result with MAX_INT, but that is very heavy handed.</p>
  
  <p>Is there just a flag that I can set either in the Cython compiler or elsewhere?</p>