|
1 | | -<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="http://localhost:4000/feed.xml" rel="self" type="application/atom+xml" /><link href="http://localhost:4000/" rel="alternate" type="text/html" /><updated>2025-12-04T17:22:50+08:00</updated><id>http://localhost:4000/feed.xml</id><title type="html">de_soot</title><subtitle>Hosted on Github Pages</subtitle><entry><title type="html">Guide to APA with groff ms and refer</title><link href="http://localhost:4000/groff-apa" rel="alternate" type="text/html" title="Guide to APA with groff ms and refer" /><published>2025-02-07T00:00:00+08:00</published><updated>2025-02-07T00:00:00+08:00</updated><id>http://localhost:4000/groff-apa</id><content type="html" xml:base="http://localhost:4000/groff-apa"><![CDATA[<p>This guide explains why and how I used groff with the ms and refer macros to write my college essay in Neovim.</p> |
| 1 | +<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="http://localhost:4000/feed.xml" rel="self" type="application/atom+xml" /><link href="http://localhost:4000/" rel="alternate" type="text/html" /><updated>2025-12-08T21:47:51+08:00</updated><id>http://localhost:4000/feed.xml</id><title type="html">de_soot</title><subtitle>Technical blog</subtitle><entry><title type="html">2’s Complement Derivation</title><link href="http://localhost:4000/2s-complement" rel="alternate" type="text/html" title="2’s Complement Derivation" /><published>2025-12-08T00:00:00+08:00</published><updated>2025-12-08T00:00:00+08:00</updated><id>http://localhost:4000/2s-complement</id><content type="html" xml:base="http://localhost:4000/2s-complement"><![CDATA[<p>If you have ever taken CS in high school or college, you have probably been taught about how to represent negative numbers in binary using 2’s complement.</p> |
| 2 | +
|
| 3 | +<p>But teachers generally just tell you to “flip the bits and add one” without really explaining why it works—similar to how high school teachers treat math formulas.</p> |
| 4 | +
|
| 5 | +<p>I have never liked memorising without understanding when it comes to learning, so I came up with an intuitive derivation and wrote this blog post to share it in hopes that somebody may find it useful.</p> |
| 6 | +
|
| 7 | +<h2 id="how-does-2s-complement-work">How Does 2’s Complement Work?</h2> |
| 8 | +
|
| 9 | +<p>Say there is a number <code class="language-plaintext highlighter-rouge">x</code> represented by <code class="language-plaintext highlighter-rouge">n</code> binary digits (bits). |
| 10 | +Since each bit can be <code class="language-plaintext highlighter-rouge">0</code> or <code class="language-plaintext highlighter-rouge">1</code>, there are 2^n possible combinations for the string of bits that represent values ranging from zero to 2^n - 1, and thus <code class="language-plaintext highlighter-rouge">x</code> is always less than 2^n.</p> |
| 11 | +
|
| 12 | +<p>If we flip all the bits of <code class="language-plaintext highlighter-rouge">x</code> using the bitwise NOT operator (<code class="language-plaintext highlighter-rouge">~</code>), we get a new number with <code class="language-plaintext highlighter-rouge">1</code> bits where there were <code class="language-plaintext highlighter-rouge">0</code>’s in <code class="language-plaintext highlighter-rouge">x</code> and <code class="language-plaintext highlighter-rouge">0</code> bits where there were <code class="language-plaintext highlighter-rouge">1</code>’s in <code class="language-plaintext highlighter-rouge">x</code>. |
| 13 | +We will call this new number <code class="language-plaintext highlighter-rouge">~x</code>, where <code class="language-plaintext highlighter-rouge">x + ~x = 2^n - 1</code>, since 2^n - 1 has all <code class="language-plaintext highlighter-rouge">n</code> number of bits set to <code class="language-plaintext highlighter-rouge">1</code>.</p> |
| 14 | +
|
| 15 | +<p>Rearranging the above equation for <code class="language-plaintext highlighter-rouge">x</code>, we get <code class="language-plaintext highlighter-rouge">x = (2^n - 1) - ~x</code>. |
| 16 | +It is then easy to find that <code class="language-plaintext highlighter-rouge">-x = -(2^n - 1 - ~x) = -2^n + 1 + ~x</code>. |
| 17 | +Now see the picture of where “flip the bits and add one” comes from.</p> |
| 18 | +
|
| 19 | +<p>But we cannot just flip bits and add 1 to get <code class="language-plaintext highlighter-rouge">-x</code> with ordinary bits, that would just be <code class="language-plaintext highlighter-rouge">~x + 1</code>. |
| 20 | +We are still missing the <code class="language-plaintext highlighter-rouge">-2^n</code> in the equation, but how would we get that?</p> |
| 21 | +
|
| 22 | +<p>Currently, since the value of the most significant bit is <code class="language-plaintext highlighter-rouge">2^(n-1)</code>, if we add another bit from the left, its place value would be <code class="language-plaintext highlighter-rouge">2^(n-1) * 2 = 2^n</code>. |
| 23 | +Now all we have to do is define the value of that new bit to be negative to get <code class="language-plaintext highlighter-rouge">-2^n</code>. |
| 24 | +When we flip the bits of <code class="language-plaintext highlighter-rouge">x</code>, this new bit we added will also get flipped along with it.</p> |
| 25 | +
|
| 26 | +<h2 id="conclusion">Conclusion</h2> |
| 27 | +
|
| 28 | +<p>Putting all the pieces together, we get that for a number <code class="language-plaintext highlighter-rouge">x</code> represented by <code class="language-plaintext highlighter-rouge">n</code> bits (with an extra bit having a place value of <code class="language-plaintext highlighter-rouge">-2^n</code>) where <code class="language-plaintext highlighter-rouge">x = (2^n - 1) - ~x</code>, you would need to flip its bits and add one to get <code class="language-plaintext highlighter-rouge">-2^n + ~x + 1 = -x</code>.</p> |
| 29 | +
|
| 30 | +<p>Hopefully after reading this, you will have gained a better understanding of using 2’s complement for converting binary numbers to negative.</p>]]></content><author><name></name></author><category term="cs," /><category term="binary," /><category term="bits," /><category term="negative" /><category term="numbers," /><category term="derivation," /><category term="proof," /><category term="math," /><category term="2's" /><category term="complement" /><summary type="html"><![CDATA[If you have ever taken CS in high school or college, you have probably been taught about how to represent negative numbers in binary using 2’s complement.]]></summary></entry><entry><title type="html">Guide to APA with groff ms and refer</title><link href="http://localhost:4000/groff-apa" rel="alternate" type="text/html" title="Guide to APA with groff ms and refer" /><published>2025-02-20T00:00:00+08:00</published><updated>2025-02-20T00:00:00+08:00</updated><id>http://localhost:4000/groff-apa</id><content type="html" xml:base="http://localhost:4000/groff-apa"><![CDATA[<p>This guide explains why and how I used groff with the ms and refer macros to write my college essay in Neovim.</p> |
2 | 31 |
|
3 | 32 | <h1 id="table-of-contents-">Table of Contents <a name="tableofcontents"></a></h1> |
4 | 33 |
|
|
0 commit comments