 <p>how about that:</p> <pre><code>value = value &gt; 0 ? value: ~value + 1 </code></pre> <p>its based on the fact that negative numbers are stored as 2's complement to there positive equivalent, and that one can build the 2's complement by first building the 1's complement and adding 1, so </p> <pre><code> 5 -&gt; 0000 0101b -5 -&gt; (1111 1010b) + 1 -&gt; 1111 1011b </code></pre> <p>what I did was basically to reverse this, so</p> <pre><code>-5 -&gt; 1111 1011b 5 -&gt; (0000 0100b) + 1 -&gt; 0000 0101b </code></pre> <p>I know it's a bit late but just had the same issue and landed here, hope this helps.</p>