title: conversion of uint8_t to sint8_t
body: <p>What's the best way to convert an "uint8_t" to an "sint8_t" in portable C.</p>
  
  <p>That's the code I came up with ....</p>
  
  <pre><code>#include &lt;stdint.h&gt;
  
  sint8_t DESER_SINT8(uint8_t x)
  (
    return
       (sint8_t)((x &gt;= (1u &lt;&lt; 8u))
                 ? -(UINT8_MAX - x)
                 : x);
  )
  </code></pre>
  
  <p>Is there a better/simpler way to do it? Maybe a way without using a conditional?</p>
  
  <p>Edit: Thanks guys. So to sum up, what I learned already ...</p>
  
  <ul>
  <li><code>sint8_t</code> is really called <code>int8_t</code></li>
  <li><code>128</code> is expressed by <code>1 &lt;&lt; 7</code> and not by <code>1 &lt;&lt; 8</code></li>
  <li>2s complement is "negating off by one"</li>
  </ul>
  
  <p>:)</p>
  
  <p>So here is an updated version of my original code:</p>
  
  <pre><code>#include &lt;stdint.h&gt;
  
  int8_t DESER_INT8(uint8_t x)
  (
    return ((x &gt;= (1 &lt;&lt; 7))
            ? -(UINT8_MAX - x + 1)
            : x);
  )
  </code></pre>