Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A <a href="https://en.wikipedia.org/wiki/Bitwise_NOT#NOT" rel="nofollow">bitwise NOT</a> is also known as a <a href="https://en.wikipedia.org/wiki/1s_complement" rel="nofollow">1s complement</a>.</p> <p>You can convert a 1s complement number to <a href="https://en.wikipedia.org/wiki/Two%27s_complement" rel="nofollow">2s complement</a> by adding 1.</p> <p>Both 1s complement and 2s complement are different ways of representing negative numbers, but these days 2s complement is almost universally used.</p> <p>So, to negate a number in 2s complement form you can simply apply a bitwise NOT and add 1, i.e.</p> <pre><code>-x == ~x + 1 </code></pre> <p>Conversely, to convert a 2s complement number to 1s complement you can negate it and subtract 1:</p> <pre><code>~x == -x - 1 </code></pre> <p>Note that positive numbers have the same representation in 1s complement and 2s complement representations - it is only negative numbers for which there is a difference. E.g. for 8 bit integer:</p> <pre><code>Decimal 1s complement 2s complement 0 00000000 00000000 -0 11111111 n/a 1 00000001 00000001 -1 11111110 11111111 2 00000010 00000010 -2 11111101 11111110 127 01111111 01111111 -127 10000000 10000001 -128 n/a 10000000 </code></pre> <p>Note that 1s complement has two representations for decimal 0: +0 and -0, while 2s complement has a unique zero representation. 2s complement can also represent an additional negative value that is not available in 1s complement (-128 in the case of 8 bit integers).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload