mirror of
https://github.com/krahets/hello-algo.git
synced 2026-12-01 08:45:02 +08:00
Release reviewed gh-pages candidate
This commit is contained in:
@@ -1925,8 +1925,8 @@
|
||||
<li><strong>Two's complement</strong>: A positive number again has the same representation as sign-magnitude. For a negative number, add <span class="arithmatex">\(1\)</span> to its one's complement.</li>
|
||||
</ul>
|
||||
<p>Figure 3-4 shows how to convert among these representations.</p>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_data_structure/number_encoding.assets/1s_2s_complement.png" data-dark-src="/assets/figure-svg/figures/98801e8e5cb03cae20115983.json" data-src="/assets/figure-svg/figures/dd4b9a4dcae05977bb4b6e51.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_data_structure/number_encoding.assets/1s_2s_complement.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Converting between sign-magnitude, one's complement, and two's complement</object><object data="/en/chapter_data_structure/number_encoding.assets/1s_2s_complement.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Converting between sign-magnitude, one's complement, and two's complement</object></ha-svg-figure></p>
|
||||
<p align="center"> Figure 3-4 Converting between sign-magnitude, one's complement, and two's complement </p>
|
||||
<p><ha-svg-figure aria-label="Eight-bit sign-magnitude, one's complement, and two's complement" class="animation-figure" data-book-key="en/chapter_data_structure/number_encoding.assets/1s_2s_complement.png" data-dark-src="/assets/figure-svg/figures/98801e8e5cb03cae20115983.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/dd4b9a4dcae05977bb4b6e51.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_data_structure/number_encoding.assets/1s_2s_complement.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">Converting between sign-magnitude, one's complement, and two's complement</object><object aria-hidden="true" data="/en/chapter_data_structure/number_encoding.assets/1s_2s_complement.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">Converting between sign-magnitude, one's complement, and two's complement</object></ha-svg-figure></p>
|
||||
<p align="center"> Figure 3-4 Converting between sign-magnitude, one's complement, and two's complement </p><div class="ha-sr-only figure-description"><h3>Description of Figure 3-4: Eight-bit sign-magnitude, one's complement, and two's complement</h3><p>Compare how positive and negative integers convert across the three encodings, and see why two's complement has only one representation of zero.</p><p>The figure arranges sign-magnitude, one's complement, and two's complement in three rows comparing +0, +5, −0, and −28, each written as 8 bits grouped into two 4-bit nibbles. Bidirectional arrows connect sign-magnitude with one's complement and one's complement with two's complement; the notes on the right state the forward rules for converting a negative number from sign-magnitude to one's complement and then to two's complement.</p><table><caption>Eight-bit encoding comparison</caption><thead><tr><th scope="col">Number</th><th scope="col">Sign-magnitude</th><th scope="col">One's complement</th><th scope="col">Two's complement</th></tr></thead><tbody><tr><th scope="row">+0</th><td>0000 0000</td><td>0000 0000</td><td>0000 0000</td></tr><tr><th scope="row">+5</th><td>0000 0101</td><td>0000 0101</td><td>0000 0101</td></tr><tr><th scope="row">−0</th><td>1000 0000</td><td>1111 1111</td><td>0000 0000</td></tr><tr><th scope="row">−28</th><td>1001 1100</td><td>1110 0011</td><td>1110 0100</td></tr></tbody></table><p>The highest bit is the sign bit, and +0 and +5 have identical bit patterns across all three representations. For a negative number, keep the leading sign bit 1 and invert only the lower 7 bits to get the one's complement, then add 1 and keep the lower 8 bits to get the two's complement. In the table, −28 follows this rule; the reverse conversion subtracts 1 from the two's complement and then inverts the lower 7 bits while keeping the sign bit to return to sign-magnitude. Adding 1 to the one's complement of −0 (1111 1111) produces the nine-bit value 1 0000 0000; discarding the carry-out leaves 0000 0000, identical to +0, so positive zero and negative zero can no longer be distinguished from the two's complement.</p></div>
|
||||
<p>Although <u>sign-magnitude</u> is intuitive, it has limitations. First, <strong>negative sign-magnitude values cannot be used directly in ordinary binary arithmetic</strong>. For example, directly adding <span class="arithmatex">\(1 + (-2)\)</span> in this representation gives <span class="arithmatex">\(-3\)</span>, which is clearly wrong.</p>
|
||||
<div class="arithmatex">\[
|
||||
\begin{aligned}
|
||||
@@ -2006,8 +2006,8 @@ b_{31} b_{30} b_{29} \ldots b_2 b_1 b_0
|
||||
(1 + \mathrm{N}) = & (1 + \sum_{i=1}^{23} b_{23-i} 2^{-i}) \subset [1, 2 - 2^{-23}]
|
||||
\end{aligned}
|
||||
\]</div>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_data_structure/number_encoding.assets/ieee_754_float.png" data-dark-src="/assets/figure-svg/figures/a6e0e9fba706ed40e99b3bb2.json" data-src="/assets/figure-svg/figures/950abea682674cbde9ec1917.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_data_structure/number_encoding.assets/ieee_754_float.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Example of an IEEE 754 float calculation</object><object data="/en/chapter_data_structure/number_encoding.assets/ieee_754_float.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Example of an IEEE 754 float calculation</object></ha-svg-figure></p>
|
||||
<p align="center"> Figure 3-5 Example of an IEEE 754 float calculation </p>
|
||||
<p><ha-svg-figure aria-label="Bit fields of a single-precision floating-point number" class="animation-figure" data-book-key="en/chapter_data_structure/number_encoding.assets/ieee_754_float.png" data-dark-src="/assets/figure-svg/figures/a6e0e9fba706ed40e99b3bb2.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/950abea682674cbde9ec1917.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_data_structure/number_encoding.assets/ieee_754_float.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">Example of an IEEE 754 float calculation</object><object aria-hidden="true" data="/en/chapter_data_structure/number_encoding.assets/ieee_754_float.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">Example of an IEEE 754 float calculation</object></ha-svg-figure></p>
|
||||
<p align="center"> Figure 3-5 Example of an IEEE 754 float calculation </p><div class="ha-sr-only figure-description"><h3>Description of Figure 3-5: Bit fields of a single-precision floating-point number</h3><p>Split a 32-bit encoding into sign, exponent, and fraction fields, then evaluate the number it represents.</p><p>This example shows a 4-byte IEEE 754 single-precision float. The 32 bit cells are arranged left to right from bit 31 down to bit 0, with brackets marking the orange sign bit S, the blue exponent E, and the green fraction N.</p><p>The table below lists each field and its stored bits first. Bit positions are indexed starting from bit 0 at the rightmost end; b31, b30, b23, b22, and b0 are labeled at the field boundaries in the figure.</p><table><caption>Fields in the 32-bit encoding and their meanings</caption><thead><tr><th scope="col">Field</th><th scope="col">Bit range</th><th scope="col">Bit width</th><th scope="col">Binary in example</th><th scope="col">Interpretation</th></tr></thead><tbody><tr><th scope="row">Sign bit S</th><td>Bit 31</td><td>1</td><td>0</td><td>Positive sign</td></tr><tr><th scope="row">Exponent E</th><td>Bits 30 to 23</td><td>8</td><td>01111100</td><td>Stored biased exponent is decimal 124</td></tr><tr><th scope="row">Fraction N</th><td>Bits 22 to 0</td><td>23</td><td>01100000000000000000000</td><td>First 3 bits are 011, remaining 20 bits are 0</td></tr></tbody></table><p>Because E lies between 1 and 254, the normal-number formula applies: the sign factor is +1; the unbiased exponent is 124 − 127 = −3, so the scaling factor is 2 to the power of −3, which equals 0.125. In the fraction, only the 2nd and 3rd bits after the binary point are 1, giving N = 2 to the power of −2 + 2 to the power of −3 = 0.375; adding the implicit leading 1 yields 1.375. Multiplying all three factors gives 1 × 0.125 × 1.375 = 0.171875, matching the calculation at the bottom right of the figure.</p><p>The normal-number formula is (−1)^S × 2^(E−127) × (1+N), that is, negative one to the power S, times 2 to the power (E minus 127), times (1 plus N). When E is 0 or 255, dedicated rules govern zero, subnormal numbers, infinity, and NaN, so this formula cannot be applied directly. The exponent widens the representable range without increasing significand precision: a normal float has 23 fraction bits plus the implicit 1, giving 24 bits of binary precision, so adjacent representable numbers generally grow farther apart as magnitude increases.</p></div>
|
||||
<p>In Figure 3-5, <span class="arithmatex">\(\mathrm{S} = 0\)</span>, <span class="arithmatex">\(\mathrm{E} = 124\)</span>, and <span class="arithmatex">\(\mathrm{N} = 2^{-2} + 2^{-3} = 0.375\)</span>, giving:</p>
|
||||
<div class="arithmatex">\[
|
||||
\text { val } = (-1)^0 \times 2^{124 - 127} \times (1 + 0.375) = 0.171875
|
||||
@@ -2171,5 +2171,5 @@ b_{31} b_{30} b_{29} \ldots b_2 b_1 b_0
|
||||
<script src="../../javascripts/mathjax.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.min.js"></script>
|
||||
<script src="../../javascripts/animation_player.js"></script>
|
||||
<script defer="" src="/assets/figure-svg/js/bcdea376f01bc2bc0cac7c6c.js"></script><script defer="" src="/assets/figure-svg/js/e84f59519170c0f5ef238548.js"></script><script defer="" src="/assets/figure-svg/js/a704d8e80a80ddc71f2a03a7.js"></script><script defer="" src="/assets/figure-svg/js/bff17b8259339758cb1c4a06.js"></script></body>
|
||||
<script defer="" src="/assets/figure-svg/js/00174ff861e190f5c1f09e34.js"></script><script defer="" src="/assets/figure-svg/js/e84f59519170c0f5ef238548.js"></script><script defer="" src="/assets/figure-svg/js/a704d8e80a80ddc71f2a03a7.js"></script><script defer="" src="/assets/figure-svg/js/70ae82c6406c9bdbda8e8362.js"></script></body>
|
||||
</html>
|
||||
Binary file not shown.
Reference in New Issue
Block a user