{
  "version": "https://jsonfeed.org/version/1",
  "title": "Longform on Nathan Heller",
  "icon": "https://avatars.micro.blog/avatars/2026/01/1859195.jpg",
  "home_page_url": "https://blog.naheller.com/",
  "feed_url": "https://blog.naheller.com/feed.json",
  "items": [
      {
        "id": "http://nxte.micro.blog/2026/05/24/twos-complement/",
        "title": "Two's Complement",
        "content_html": "<p>Two&rsquo;s Complement is a way to represent positive and negative numbers in binary. It&rsquo;s useful because we want a way to represent each number&rsquo;s inverse within a limited number of bits.</p>\n<h2 id=\"the-sign-bit\">The sign bit</h2>\n<p>Putting aside two&rsquo;s complement, the &ldquo;naive&rdquo; way to represent a negative number is to simply use its highest order bit as a &ldquo;sign&rdquo; bit. That is, <code>0</code> for positive and <code>1</code> for negative.</p>\n<p>With 4 bits</p>\n<pre tabindex=\"0\"><code>5 = 0101\n-5 = 1101\n</code></pre><p>With 8 bits:</p>\n<pre tabindex=\"0\"><code> 5 = 00000101\n-5 = 10000101\n</code></pre><p>However with this method, arithmetic operations come out wrong. for example:</p>\n<pre tabindex=\"0\"><code>5 + (-5) = 2\n\n 0101\n+1101\n-----\n10010 = 2\n</code></pre><p>We also have the curious existence of <code>-0</code>, if we flip the sign bit on <code>0</code>:</p>\n<pre tabindex=\"0\"><code> 0 = 0000\n-0 = 1000\n</code></pre><p>So we need a better method.</p>\n<h2 id=\"ones-complement\">One&rsquo;s Complement</h2>\n<p>This method inverts all the bits in a number to get its positive/negative equivalent, or &ldquo;complement&rdquo;. The reason it&rsquo;s called <em>One&rsquo;s</em> Complement is that adding a number to its inverse results in a sequence of all ones: <code>1111</code>.</p>\n<p>Notice that left-most bit still acts as a sign bit:</p>\n<pre tabindex=\"0\"><code> 5 = 0101\n-5 = 1010\n\n 7 = 0111\n-7 = 1000\n</code></pre><p>Unfortunately we still have <code>-0</code>, but arithmetic operations are <em>more</em> correct than before:</p>\n<pre tabindex=\"0\"><code> 0 = 0000\n-0 = 1111\n\n5 + (-5) = -0\n\n 0101\n+1010\n-----\n 1111 = -0\n</code></pre><p>In fact, arithmetic results seem to be off by one:</p>\n<pre tabindex=\"0\"><code>5 + (-3) = 1\n\n   0101\n+  1100\n-------\n(1)0001 = 1 (should be 2)\n\n6 + (-2) = 3\n\n   0110\n+  1101\n-------\n(1)0011 = 3 (should be 4)\n</code></pre><p>By adding 1 to any of the above results, we can get the correct result.</p>\n<h2 id=\"twos-complement\">Two&rsquo;s Complement</h2>\n<p>With Two&rsquo;s Complement we get rid of <code>-0</code>, which shifts the negative numbers over by one. For example, <code>-1</code> in One&rsquo;s Complement is <code>1110</code>, whereas in Two&rsquo;s Complement it is <code>1111</code>. This has the effect of &ldquo;incrementing&rdquo; it in binary, thereby giving us the added 1 we wanted in order to achieve correct arithmetic results.</p>\n<p>This method is called <em>Two&rsquo;s</em> Complement because when adding a number to its inverse, each addition operation results in the binary value for 2. This effectively &ldquo;carries the one&rdquo; all the way over to the left and into the overflow, giving us a sequence of zeroes. And any number added to its inverse should indeed be zero.</p>\n<pre tabindex=\"0\"><code>5 + (-5) = 0\n\n   0101\n+  1011\n-------\n(1)0000 = 0\n</code></pre><p>Another interesting property of Two&rsquo;s Complement is the place values. From right to left in a 4-bit sequence, we have 1&rsquo;s, 2&rsquo;s, and 4&rsquo;s places as expected. However the final bit is actually the -8&rsquo;s place.</p>\n<p>For example:</p>\n<pre tabindex=\"0\"><code>1000    -8 (-8 + 0 + 0 + 0) = -8\n1001    -7 (-8 + 0 + 0 + 1) = -7\n1010    -6 (-8 + 0 + 2 + 0) = -6\n.\n.\n.\n1111    -1 (-8 + 4 + 2 + 1) = -1\n</code></pre><p>So in addition to acting as the sign bit, that left-most bit also has mathematical meaning, which is what allows the arithmetic to work out.</p>\n<h3 id=\"inverting-a-number-in-twos-complement\">Inverting a number in Two&rsquo;s Complement</h3>\n<p>To invert a number in Two&rsquo;s Complement, we need to flip its bits and add one. This is one more step than with One&rsquo;s Complement, where we only need to flip the bits.</p>\n<pre tabindex=\"0\"><code>0101 = 5\n1010 (flip bits)\n1011 = -5 (add one)\n</code></pre><p>This is fairly trivial, but something that hardware designers need to keep in mind when building circuits that perform arithmetic.</p>\n",
        "date_published": "2026-05-24T12:54:44+07:00",
        "url": "https://blog.naheller.com/2026/05/24/twos-complement/",
        "tags": ["Longform"]
      }
  ]
}
