<story><title>Finally getting two&apos;s complement</title><url>https://neugierig.org/software/blog/2023/06/twos-complement.html</url></story><parent_chain><item><author>masswerk</author><text>I think, you can&amp;#x27;t address two&amp;#x27;s complement without mentioning ones&amp;#x27; complement.&lt;p&gt;So what is ones&amp;#x27; complement? Simply all bits flipped. (XOR the word length – each bit becomes its complement.) There&amp;#x27;s a simplicity and beauty to this and math just works with addition and subtraction. There&amp;#x27;s also the notable feature of the most significant bit, which, if excluded from the usable range of numbers, becomes the sign-bit. If it is empty (clear), it must be a positive number, if it&amp;#x27;s set, we have a negative number (since it must be a clear bit flipped).&lt;p&gt;So everything perfect, then? Not at all. What happens, if we flip all bits on zero (0000)? Well, it becomes all bits set (1111). How do we convert a number to it&amp;#x27;s signed counterpart? We flip all bits and … Oh, this must be negative zero. Maybe we can deal with this? Sort of. But it&amp;#x27;s somewhat nasty, because we need an extra steps to traverse zero. Say, we go from +1 to -1, there isn&amp;#x27;t just a zero in between, making this a difference of 2 – as it should be –, but there&amp;#x27;s +0 and -0. Three steps. That&amp;#x27;s odd.&lt;p&gt;Can we do something about this? Namely, can we get rid of negative zero? Well, as we&amp;#x27;ve seen, we have an extra step on the negative side of things… what about just adding 1 to compensate for this? So, for -1, we wouldn&amp;#x27;t write 1110 (flipping all bits of 0001), but 1111? Just the same, for -2, we would add 1 to 1101, making this 1110, and so on. – Well, this works! we just eliminated negative zero! Welcome to two&amp;#x27;s complement.&lt;p&gt;(So, is it now perfect? Well, sort of. We now have introduced a certain asymmetry into our number system: on the side of things, where the sign-bit is clear, zero is the first number and +1 the second one, while numbers with the sign-bit set start with -1. Thus, we have an excess number on the negative side of the number range described by our word length. We just can&amp;#x27;t have it all. Well, we could switch to balanced ternary, but this is another story…)</text></item></parent_chain><comment><author>FullstakBlogger</author><text>Why does starting with ones&amp;#x27; compliment make things easier? IMV, it makes it more confusing, because two&amp;#x27;s compliment seems like an arbitrary leap that happens to work.&lt;p&gt;If you&amp;#x27;re tasked with mapping a subset of bit-states to negative numbers, it&amp;#x27;s intuitively obvious that there&amp;#x27;s already a state that yields 0 when you add 1 to it, as long as you wrap on overflow. With four bits that&amp;#x27;s 1111. That&amp;#x27;s a great representation of -1.&lt;p&gt;If you want to get 0 by adding 2, you&amp;#x27;d have to start from 1110, and so on.&lt;p&gt;Just because the top bit can be used to identify a negative number when you use up half the states, doesn&amp;#x27;t mean it should be thought of as a logical flag. That&amp;#x27;s how you invent ones&amp;#x27; compliment.&lt;p&gt;I think of ones&amp;#x27; compliment as a logical encoding, and two&amp;#x27;s compliment as an arithmetic encoding, and it shows in how easy negation is in ones&amp;#x27; compliment, and how easy arithmetic is in two&amp;#x27;s compliment.&lt;p&gt;The fact that everyone tries to teach them as if one is a precursor to the other is what&amp;#x27;s confusing.</text></comment>
<story><title>Finally getting two&apos;s complement</title><url>https://neugierig.org/software/blog/2023/06/twos-complement.html</url></story><parent_chain><item><author>masswerk</author><text>I think, you can&amp;#x27;t address two&amp;#x27;s complement without mentioning ones&amp;#x27; complement.&lt;p&gt;So what is ones&amp;#x27; complement? Simply all bits flipped. (XOR the word length – each bit becomes its complement.) There&amp;#x27;s a simplicity and beauty to this and math just works with addition and subtraction. There&amp;#x27;s also the notable feature of the most significant bit, which, if excluded from the usable range of numbers, becomes the sign-bit. If it is empty (clear), it must be a positive number, if it&amp;#x27;s set, we have a negative number (since it must be a clear bit flipped).&lt;p&gt;So everything perfect, then? Not at all. What happens, if we flip all bits on zero (0000)? Well, it becomes all bits set (1111). How do we convert a number to it&amp;#x27;s signed counterpart? We flip all bits and … Oh, this must be negative zero. Maybe we can deal with this? Sort of. But it&amp;#x27;s somewhat nasty, because we need an extra steps to traverse zero. Say, we go from +1 to -1, there isn&amp;#x27;t just a zero in between, making this a difference of 2 – as it should be –, but there&amp;#x27;s +0 and -0. Three steps. That&amp;#x27;s odd.&lt;p&gt;Can we do something about this? Namely, can we get rid of negative zero? Well, as we&amp;#x27;ve seen, we have an extra step on the negative side of things… what about just adding 1 to compensate for this? So, for -1, we wouldn&amp;#x27;t write 1110 (flipping all bits of 0001), but 1111? Just the same, for -2, we would add 1 to 1101, making this 1110, and so on. – Well, this works! we just eliminated negative zero! Welcome to two&amp;#x27;s complement.&lt;p&gt;(So, is it now perfect? Well, sort of. We now have introduced a certain asymmetry into our number system: on the side of things, where the sign-bit is clear, zero is the first number and +1 the second one, while numbers with the sign-bit set start with -1. Thus, we have an excess number on the negative side of the number range described by our word length. We just can&amp;#x27;t have it all. Well, we could switch to balanced ternary, but this is another story…)</text></item></parent_chain><comment><author>omnicognate</author><text>The apostrophe placement is significant: it&amp;#x27;s two&amp;#x27;s complement (complement of two) but ones&amp;#x27; complement (complement of ones, plural).</text></comment>