This commit is contained in:
krahets
2024-01-07 23:43:01 +08:00
parent 547093210e
commit ca63e0e7f7
55 changed files with 651 additions and 290 deletions
@@ -1230,10 +1230,10 @@
<h1 id="33-number-encoding">3.3 &nbsp; Number Encoding *<a class="headerlink" href="#33-number-encoding" title="Permanent link">&para;</a></h1>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>In this book, chapters marked with an * symbol are optional reads. If you are short on time or find them challenging, you may skip these initially and return to them after completing the essential chapters.</p>
<p>In this book, chapters marked with an asterisk '*' are optional readings. If you are short on time or find them challenging, you may skip these initially and return to them after completing the essential chapters.</p>
</div>
<h2 id="331-integer-encoding">3.3.1 &nbsp; Integer Encoding<a class="headerlink" href="#331-integer-encoding" title="Permanent link">&para;</a></h2>
<p>In the table from the previous section, we noticed that all integer types can represent one more negative number than positive numbers, such as the <code>byte</code> range of <span class="arithmatex">\([-128, 127]\)</span>. This phenomenon, somewhat counterintuitive, is rooted in the concepts of sign-magnitude, one's complement, and two's complement encoding.</p>
<p>In the table from the previous section, we observed that all integer types can represent one more negative number than positive numbers, such as the <code>byte</code> range of <span class="arithmatex">\([-128, 127]\)</span>. This phenomenon seems counterintuitive, and its underlying reason involves knowledge of sign-magnitude, one's complement, and two's complement encoding.</p>
<p>Firstly, it's important to note that <strong>numbers are stored in computers using the two's complement form</strong>. Before analyzing why this is the case, let's define these three encoding methods:</p>
<ul>
<li><strong>Sign-magnitude</strong>: The highest bit of a binary representation of a number is considered the sign bit, where <span class="arithmatex">\(0\)</span> represents a positive number and <span class="arithmatex">\(1\)</span> represents a negative number. The remaining bits represent the value of the number.</li>
@@ -1280,8 +1280,8 @@
\end{aligned}
\]</div>
<p>Adding <span class="arithmatex">\(1\)</span> to the one's complement of negative zero produces a carry, but with <code>byte</code> length being only 8 bits, the carried-over <span class="arithmatex">\(1\)</span> to the 9<sup>th</sup> bit is discarded. Therefore, <strong>the two's complement of negative zero is <span class="arithmatex">\(0000 \; 0000\)</span></strong>, the same as positive zero, thus resolving the ambiguity.</p>
<p>One last puzzle is the <span class="arithmatex">\([-128, 127]\)</span> range for <code>byte</code>, with an additional negative number, <span class="arithmatex">\(-128\)</span>. We observe that for the interval <span class="arithmatex">\([-127, +127]\)</span>, all integers have corresponding sign-magnitude, one's complement, and two's complement, and these can be converted between each other.</p>
<p>However, <strong>the two's complement <span class="arithmatex">\(1000 \; 0000\)</span> is an exception without a corresponding sign-magnitude</strong>. According to the conversion method, its sign-magnitude would be <span class="arithmatex">\(0000 \; 0000\)</span>, which is a contradiction since this represents zero, and its two's complement should be itself. Computers designate this special two's complement <span class="arithmatex">\(1000 \; 0000\)</span> as representing <span class="arithmatex">\(-128\)</span>. In fact, the calculation of <span class="arithmatex">\((-1) + (-127)\)</span> in two's complement results in <span class="arithmatex">\(-128\)</span>.</p>
<p>One last puzzle is the <span class="arithmatex">\([-128, 127]\)</span> range for <code>byte</code>, with an additional negative number, <span class="arithmatex">\(-128\)</span>. We observe that for the interval <span class="arithmatex">\([-127, +127]\)</span>, all integers have corresponding sign-magnitude, one's complement, and two's complement, allowing for mutual conversion between them.</p>
<p>However, <strong>the two's complement <span class="arithmatex">\(1000 \; 0000\)</span> is an exception without a corresponding sign-magnitude</strong>. According to the conversion method, its sign-magnitude would be <span class="arithmatex">\(0000 \; 0000\)</span>, indicating zero. This presents a contradiction because its two's complement should represent itself. Computers designate this special two's complement <span class="arithmatex">\(1000 \; 0000\)</span> as representing <span class="arithmatex">\(-128\)</span>. In fact, the calculation of <span class="arithmatex">\((-1) + (-127)\)</span> in two's complement results in <span class="arithmatex">\(-128\)</span>.</p>
<div class="arithmatex">\[
\begin{aligned}
&amp; (-127) + (-1) \newline