﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-I am Tristan-随笔分类-c</title><link>http://www.cppblog.com/woshicl/category/9341.html</link><description>c/c++</description><language>zh-cn</language><lastBuildDate>Fri, 16 Jan 2009 00:50:36 GMT</lastBuildDate><pubDate>Fri, 16 Jan 2009 00:50:36 GMT</pubDate><ttl>60</ttl><item><title>(转)原码、反码、补码</title><link>http://www.cppblog.com/woshicl/archive/2009/01/14/71978.html</link><dc:creator>陈三石</dc:creator><author>陈三石</author><pubDate>Wed, 14 Jan 2009 03:59:00 GMT</pubDate><guid>http://www.cppblog.com/woshicl/archive/2009/01/14/71978.html</guid><wfw:comment>http://www.cppblog.com/woshicl/comments/71978.html</wfw:comment><comments>http://www.cppblog.com/woshicl/archive/2009/01/14/71978.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/woshicl/comments/commentRss/71978.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/woshicl/services/trackbacks/71978.html</trackback:ping><description><![CDATA[今天做题遇到进制转换问题，发现原来学的知识有些遗忘，百度了一下进制转换，复习了一遍。其中作者关于负数的二进制表达写的不错，故摘录于此。<br><span style="COLOR: red">在计算机中，负数以其正数的补码形式表达。<br>
<p style="COLOR: #000000">原码：一个整数，按照绝对值大小转换成的二进制数，称为原码。</p>
<p style="COLOR: #000000">比如 00000000 00000000 00000000 00000101 是 5的 原码。<br><br>反码：将二进制数按位取反，所得的新二进制数称为原二进制数的反码。</p>
<p style="COLOR: #000000">取反操作指：原为1，得0；原为0，得1。（1变0; 0变1）</p>
<p style="COLOR: #000000">比如：将00000000 00000000 00000000 00000101每一位取反，得11111111 11111111 11111111 11111010。</p>
<p style="COLOR: #000000">称：11111111 11111111 11111111 11111010 是 00000000 00000000 00000000 00000101 的反码。</p>
<p style="COLOR: #000000">反码是相互的，所以也可称：</p>
<p style="COLOR: #000000">11111111 11111111 11111111 11111010 和 00000000 00000000 00000000 00000101 互为反码。</p>
<p style="COLOR: #000000">补码：反码加1称为补码。</p>
<p style="COLOR: #000000">也就是说，要得到一个数的补码，先得到反码，然后将反码加上1，所得数称为补码。</p>
<p style="COLOR: #000000">比如：00000000 00000000 00000000 00000101 的反码是：11111111 11111111 11111111 11111010。</p>
<p style="COLOR: #000000">那么，补码为：</p>
<p style="COLOR: #000000">11111111 11111111 11111111 11111010 + 1 = 11111111 11111111 11111111 11111011</p>
<p style="COLOR: #000000">　</p>
<p style="COLOR: #000000">所以，-5 在计算机中表达为：11111111 11111111 11111111 11111011。转换为十六进制：0xFFFFFFFB。<br><br></p>
<p style="COLOR: #000000">再举一例，我们来看整数-1在计算机中如何表示。</p>
<p style="COLOR: #000000">假设这也是一个int类型，那么：　</p>
<p style="COLOR: #000000">1、先取1的原码：00000000 00000000 00000000 00000001</p>
<p style="COLOR: #000000">2、得反码：&nbsp;&nbsp;&nbsp;&nbsp; 11111111 11111111 11111111 11111110</p>
<p style="COLOR: #000000">3、得补码：&nbsp;&nbsp;&nbsp;&nbsp; 11111111 11111111 11111111 11111111</p>
<p style="COLOR: #000000">　</p>
<p style="COLOR: #000000">可见，－1在计算机里用二进制表达就是全1。16进制为：0xFFFFFF。</p>
</span>
<img src ="http://www.cppblog.com/woshicl/aggbug/71978.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/woshicl/" target="_blank">陈三石</a> 2009-01-14 11:59 <a href="http://www.cppblog.com/woshicl/archive/2009/01/14/71978.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>