13 lines
15 KiB
JavaScript
13 lines
15 KiB
JavaScript
import{_ as s,c as a,a as n,o as i}from"./app-DVyxS3f7.js";const t={};function d(o,e){return i(),a("div",null,[...e[0]||(e[0]=[n(`<p>欢迎来到 0 与 1 的魔法派对!这篇文章将带你揭开原码、反码与补码的奥秘,看计算机如何巧妙地用补码化减为加。让我们一起翻开这页,去捕捉二进制底层那份迷人的理性之美吧♪</p><h2 id="一、-前置概念" tabindex="-1"><a class="header-anchor" href="#一、-前置概念"><span>一、 前置概念</span></a></h2><p>计算机底层存储数据的时候使用的是二进制数字,但是计算机在存储一个数字的时候并不是直接存储该数字对应的二进制数,而是存储该数字对应的<strong>二进制数的补码</strong>。</p><p>在了解原码、反码和补码之前,我们要了解<strong>机器数</strong>和<strong>真值</strong>的概念。</p><h3 id="_1-机器数" tabindex="-1"><a class="header-anchor" href="#_1-机器数"><span>1) 机器数</span></a></h3><p>一个数在计算机的存储形式是二进制数,我们称这些二进制数为<strong>机器数</strong>。机器数是有符号的,在计算机中用机器数的最高位存放符号位,<code>0</code> 表示正数,<code>1</code> 表示负数。</p><h3 id="_2-真值" tabindex="-1"><a class="header-anchor" href="#_2-真值"><span>2) 真值</span></a></h3><p>因为机器数带有符号位,所以机器数的形式值不等于其真实表示的值(真值)。</p><ul><li>以机器数 <code>1000 0001</code> 为例,其真正表示的值(首位为符号位)为 <code>-1</code>,而形式值(首位就是代表 1)为 <code>129</code>。</li><li>因此将带符号的机器数的真正表示的值称为机器数的<strong>真值</strong>。</li></ul><h2 id="二、-原码、反码与补码" tabindex="-1"><a class="header-anchor" href="#二、-原码、反码与补码"><span>二、 原码、反码与补码</span></a></h2><h3 id="_1-原码" tabindex="-1"><a class="header-anchor" href="#_1-原码"><span>1) 原码</span></a></h3><p>原码的表示与机器数真值表示的一样,即用第一位表示符号,其余位表示数值。</p><ul><li><strong>正数</strong>:就是它对应的二进制数。</li><li><strong>负数</strong>:将绝对值对应的二进制最左边位变为 <code>1</code>。</li></ul><p>例如十进制的正负 1,用 8 位二进制的原码表示如下:</p><ul><li><code>[+1]</code> = 原: <code>[ 0000 0001 ]</code></li><li><code>[-1]</code> = 原: <code>[ 1000 0001 ]</code></li></ul><h3 id="_2-反码" tabindex="-1"><a class="header-anchor" href="#_2-反码"><span>2) 反码</span></a></h3><ul><li><strong>正数</strong>:和原码相同。</li><li><strong>负数</strong>:在其原码的基础上,<strong>符号位不变,其余各位取反</strong>。</li></ul><p>示例:</p><ul><li><code>[+1]</code> = 原: <code>[ 0000 0001 ]</code> = 反: <code>[ 0000 0001 ]</code></li><li><code>[-1]</code> = 原: <code>[ 1000 0001 ]</code> = 反: <code>[ 1111 1110 ]</code></li></ul><h3 id="_3-补码" tabindex="-1"><a class="header-anchor" href="#_3-补码"><span>3) 补码</span></a></h3><ul><li><strong>正数</strong>:补码是其原码本身。</li><li><strong>负数</strong>:补码是在其原码的基础上,<strong>符号位不变,其余各位取反后加 1</strong>(即在反码的基础上加 1)。</li></ul><p>示例:</p><ul><li><code>[+1]</code> = 原: <code>[ 0000 0001 ]</code> = 反: <code>[ 0000 0001 ]</code> = 补: <code>[ 0000 0001 ]</code></li><li><code>[-1]</code> = 原: <code>[ 1000 0001 ]</code> = 反: <code>[ 1111 1110 ]</code> = 补: <code>[ 1111 1111 ]</code></li></ul><h2 id="三、-数据在计算机中的存储形式" tabindex="-1"><a class="header-anchor" href="#三、-数据在计算机中的存储形式"><span>三、 数据在计算机中的存储形式</span></a></h2><p>计算机实际上只存储<strong>补码</strong>,所以说原码转换为补码的过程,也可以理解为数据存储到计算机内存中的过程。</p><p>在原、反、补码中,正数的表示是一模一样的,而负数的表示是不相同的。因此对于负数的补码来说,我们不能直接用进制转换将其转换为十进制数值,因为这样是得不到计算机真正存储的十进制数的。<strong>应该将其转换为原码后,再将转换得到的原码进行进制转换为十进制数</strong>(机器数包含符号位)。</p><h2 id="四、-为什么会使用原码、反码、补码" tabindex="-1"><a class="header-anchor" href="#四、-为什么会使用原码、反码、补码"><span>四、 为什么会使用原码、反码、补码</span></a></h2><p>对于人脑来说,知道机器数的第一位是符号位是一件很轻松的事情,但对于计算机基础电路设计来说判别第一位是符号位是非常难和复杂的事情。</p><p>为了让计算机底层设计更加简单,于是设计将符号位参与运算,并且<strong>只保留加法</strong>的方法,通过<strong>加上一个负数的方式来实现减法</strong>。这样让计算机运算更加简单,并且也让符号位参与到运算中去。</p><h2 id="五、-使用原码、反码与补码进行运算" tabindex="-1"><a class="header-anchor" href="#五、-使用原码、反码与补码进行运算"><span>五、 使用原码、反码与补码进行运算</span></a></h2><h3 id="_1-使用原码运算" tabindex="-1"><a class="header-anchor" href="#_1-使用原码运算"><span>1) 使用原码运算</span></a></h3><p>计算十进制表达式:<code>1 - 1 = 0</code></p><div class="language-text line-numbers-mode" data-highlighter="shiki" data-ext="text" style="--shiki-light:#393a34;--shiki-dark:#dbd7caee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#121212;"><pre class="shiki shiki-themes vitesse-light vitesse-dark vp-code"><code class="language-text"><span class="line"><span>1 - 1 = 1 + (-1)</span></span>
|
||
<span class="line"><span> = 原:[ 0000 0001 ] + 原:[ 1000 0001 ]</span></span>
|
||
<span class="line"><span> = 原:[ 1000 0010 ] = -2</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><strong>结论</strong>:如果用原码表示,让符号位也参与计算,对于减法来说,结果是不正确的。这也是计算机内部在存储数据时不使用原码的原因。为了解决这一问题,出现了反码。</p><h3 id="_2-使用反码运算" tabindex="-1"><a class="header-anchor" href="#_2-使用反码运算"><span>2) 使用反码运算</span></a></h3><p>计算十进制表达式:<code>1 - 1 = 0</code></p><div class="language-text line-numbers-mode" data-highlighter="shiki" data-ext="text" style="--shiki-light:#393a34;--shiki-dark:#dbd7caee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#121212;"><pre class="shiki shiki-themes vitesse-light vitesse-dark vp-code"><code class="language-text"><span class="line"><span>1 - 1 = 1 + (-1)</span></span>
|
||
<span class="line"><span> = 原:[ 0000 0001 ] + 原:[ 1000 0001 ]</span></span>
|
||
<span class="line"><span> = 反:[ 0000 0001 ] + 反:[ 1111 1110 ]</span></span>
|
||
<span class="line"><span> = 反:[ 1111 1111 ] = 原:[ 1000 0000 ] = -0</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><strong>结论</strong>:通过计算我们发现用反码计算减法,结果的真值部分是正确的。唯一的问题出现在 "0" 这个特殊的数值上。虽然人们理解上 <code>+0</code> 和 <code>-0</code> 是一样的,但是 0 带符号是没有任何意义的,而且会有 <code>[0000 0000]原</code> 和 <code>[1000 0000]原</code> 两个编码表示 0。为了解决这一问题,出现了补码。</p><h3 id="_3-使用补码运算" tabindex="-1"><a class="header-anchor" href="#_3-使用补码运算"><span>3) 使用补码运算</span></a></h3><p>计算十进制表达式:<code>1 - 1 = 0</code></p><div class="language-text line-numbers-mode" data-highlighter="shiki" data-ext="text" style="--shiki-light:#393a34;--shiki-dark:#dbd7caee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#121212;"><pre class="shiki shiki-themes vitesse-light vitesse-dark vp-code"><code class="language-text"><span class="line"><span>1 - 1 = 1 + (-1)</span></span>
|
||
<span class="line"><span> = 原:[ 0000 0001 ] + 原:[ 1000 0001 ]</span></span>
|
||
<span class="line"><span> = 补:[ 0000 0001 ] + 补:[ 1111 1111 ]</span></span>
|
||
<span class="line"><span> = 补:[ 0000 0000 ] = 原:[ 0000 0000 ] = 0</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><strong>结论</strong>:这样 0 用 <code>[0000 0000]</code> 表示,而以前出现问题的 <code>-0</code> 则不存在了。而且人们还发现可以用 <code>[1000 0000]</code> 表示 <code>-128</code>。</p><p><strong>-128 的推算过程如下</strong>:</p><div class="language-text line-numbers-mode" data-highlighter="shiki" data-ext="text" style="--shiki-light:#393a34;--shiki-dark:#dbd7caee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#121212;"><pre class="shiki shiki-themes vitesse-light vitesse-dark vp-code"><code class="language-text"><span class="line"><span>(-1) + (-127) = -128</span></span>
|
||
<span class="line"><span> = 原:[ 1000 0001 ] + 原:[ 1111 1111 ]</span></span>
|
||
<span class="line"><span> = 补:[ 1111 1111 ] + 补:[ 1000 0001 ]</span></span>
|
||
<span class="line"><span> = 补:[ 1000 0000 ]</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote><p><strong>注意</strong>:因为实际上是使用以前的 <code>-0</code> 的补码来表示 <code>-128</code>,所以 <code>-128</code> 并没有原码和反码表示。只要补码是 <code>[1000 0000]</code>,其十进制数值就为 <code>-128</code>。</p></blockquote><h3 id="_4-小结" tabindex="-1"><a class="header-anchor" href="#_4-小结"><span>4) 小结</span></a></h3><p>因为补码能多存储一个 <code>-128</code>,而且在计算机底层中存储的是补码,所以在计算机中一个 8 位的二进制数的存储范围是用补码表示的 <code>[-128, 127]</code>,而不是用原码或反码表示的 <code>[-127, 127]</code>。这也可以解释为什么计算机中一个字节的取值范围是 <code>[-128, 127]</code>。</p><h2 id="六、-总结-牢记" tabindex="-1"><a class="header-anchor" href="#六、-总结-牢记"><span>六、 总结(牢记)</span></a></h2><ul><li><strong>二进制的最高位是符号位</strong>:<code>0</code> 表示正数,<code>1</code> 表示负数。</li><li><strong>正数三码合一</strong>:正数的原码、反码、补码都一样。</li><li><strong>负数反码</strong>:它的原码符号位不变,其它位取反。</li><li><strong>负数补码</strong>:它的反码 + 1;反之,负数反码 = 负数补码 - 1。</li><li><strong>0 的表示</strong>:<code>0</code> 的反码、补码都是 <code>0</code>。</li><li><strong>运算方式</strong>:在计算机运算的时候都是以 <strong>“补码”</strong> 的方式来运算的。</li><li><strong>查看结果</strong>:当我们看运算结果的时候,要看它的<strong>原码</strong>(重点)。</li></ul>`,49)])])}const r=s(t,[["render",d]]),l=JSON.parse('{"path":"/archives/6f41cabe-41e6-4a09-9f1c-af7dd709a35d/","title":"原码、反码、补码","lang":"zh-CN","frontmatter":{"title":"原码、反码、补码","createTime":"2026/01/08 16:34:05","cover":"/images/elysia/6.jpg","coverStyle":{"layout":"right"},"permalink":"/archives/6f41cabe-41e6-4a09-9f1c-af7dd709a35d/","description":"欢迎来到 0 与 1 的魔法派对!这篇文章将带你揭开原码、反码与补码的奥秘,看计算机如何巧妙地用补码化减为加。让我们一起翻开这页,去捕捉二进制底层那份迷人的理性之美吧♪ 一、 前置概念 计算机底层存储数据的时候使用的是二进制数字,但是计算机在存储一个数字的时候并不是直接存储该数字对应的二进制数,而是存储该数字对应的二进制数的补码。 在了解原码、反码和补...","head":[["script",{"type":"application/ld+json"},"{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Article\\",\\"headline\\":\\"原码、反码、补码\\",\\"image\\":[\\"https://www.simengweb.com/images/elysia/6.jpg\\"],\\"dateModified\\":\\"2026-01-08T09:05:22.000Z\\",\\"author\\":[]}"],["meta",{"property":"og:url","content":"https://www.simengweb.com/archives/6f41cabe-41e6-4a09-9f1c-af7dd709a35d/"}],["meta",{"property":"og:site_name","content":"仲夏夜之梦"}],["meta",{"property":"og:title","content":"原码、反码、补码"}],["meta",{"property":"og:description","content":"欢迎来到 0 与 1 的魔法派对!这篇文章将带你揭开原码、反码与补码的奥秘,看计算机如何巧妙地用补码化减为加。让我们一起翻开这页,去捕捉二进制底层那份迷人的理性之美吧♪ 一、 前置概念 计算机底层存储数据的时候使用的是二进制数字,但是计算机在存储一个数字的时候并不是直接存储该数字对应的二进制数,而是存储该数字对应的二进制数的补码。 在了解原码、反码和补..."}],["meta",{"property":"og:type","content":"article"}],["meta",{"property":"og:image","content":"https://www.simengweb.com/images/elysia/6.jpg"}],["meta",{"property":"og:locale","content":"zh-CN"}],["meta",{"property":"og:updated_time","content":"2026-01-08T09:05:22.000Z"}],["meta",{"name":"twitter:card","content":"summary_large_image"}],["meta",{"name":"twitter:image:src","content":"https://www.simengweb.com/images/elysia/6.jpg"}],["meta",{"name":"twitter:image:alt","content":"原码、反码、补码"}],["meta",{"property":"article:modified_time","content":"2026-01-08T09:05:22.000Z"}]]},"readingTime":{"minutes":5.24,"words":1573},"git":{"createdTime":1767863122000,"updatedTime":1767863122000,"contributors":[{"name":"祀梦","username":"","email":"3501646051@qq.com","commits":1,"avatar":"https://gravatar.com/avatar/6406a81eeddc359cf3d3ce018797689fc6d014ff06215c27d0210b42e8f5a8ab?d=retro"}]},"autoDesc":true,"filePathRelative":"blog/technology/signed-binary-representations.md","headers":[],"categoryList":[{"id":"126ac9","sort":10000,"name":"blog"},{"id":"750eb7","sort":10001,"name":"technology"}]}');export{r as comp,l as data};
|