title: How to do C language calculation in Python
body: <p>I want to do some simulation of C language calculation in Python.
  For example, unsigned short, single precision float ... </p>
  
  <pre><code>ushort(0xffff) + 1 -&gt; 0
  0.1f * 0.1f -&gt; ...
  </code></pre>
  
  <p>Are there some library to do this in Python?</p>
  
  <p>I can use ctypes to create unsigned short, single float, but they
  cann't do math operation:</p>
  
  <pre><code>a = c_uint16(0xffff)
  b = c_uint16(0x01)
  a+b -&gt; TypeError
  </code></pre>
  
  <p>Or, I can use numpy:</p>
  
  <pre><code>&gt;&gt;&gt; np.uint16(0xffff) + np.uint16(0x01)
  Warning: overflow encountered in ushort_scalars
  0
  </code></pre>
  
  <p>but it's very slow comparing to Python's normal calculation:</p>
  
  <pre><code>&gt;&gt;&gt; timeit.timeit("a+b", "import numpy as np;a=np.uint16(0xfffe);b=np.uint16(0x01)")
  0.35577465681618037
  &gt;&gt;&gt; timeit.timeit("0xfffe+0x01")
  0.022638104432360251
  &gt;&gt;&gt; timeit.timeit("np.uint16(0xfffe) + np.uint16(0x01)", "import numpy as np")
  5.904765399236851  
  </code></pre>
  
  <p>Edit:</p>
  
  <pre><code>&gt;&gt;&gt; timeit.timeit("a+b", "a=0xfffe;b=0x01")
  0.040062221014295574  
  </code></pre>