<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Another My Program &#187; 數學</title>
	<atom:link href="http://tim.diary.tw/category/%e6%95%b8%e5%ad%b8/feed/" rel="self" type="application/rss+xml" />
	<link>http://tim.diary.tw</link>
	<description>just another my program</description>
	<lastBuildDate>Sun, 29 Jan 2012 10:40:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<atom:link rel='hub' href='http://tim.diary.tw/?pushpress=hub'/>
		<item>
		<title>二的補數來表現負數</title>
		<link>http://tim.diary.tw/2009/08/24/twos-complement/</link>
		<comments>http://tim.diary.tw/2009/08/24/twos-complement/#comments</comments>
		<pubDate>Mon, 24 Aug 2009 08:58:30 +0000</pubDate>
		<dc:creator>tim</dc:creator>
				<category><![CDATA[數學]]></category>
		<category><![CDATA[arithmetic]]></category>
		<category><![CDATA[binary]]></category>
		<category><![CDATA[computer]]></category>
		<category><![CDATA[negative]]></category>

		<guid isPermaLink="false">http://tim.diary.tw/?p=86</guid>
		<description><![CDATA[二進位負數]]></description>
			<content:encoded><![CDATA[<p>這是一個二進位的小問題. 在電腦的世界裡, 使用二進位表示法來存放數字是再單純不過的. 不過數字為能有更佳的效率, 有一些設計是很重要的, 例如用二的補數來表示負數, 以下為一個例子.</p>
<p>假設用一個 byte 來代表數字的話, 則共有8個bit可用, 最高字元保留給符號, 而數字就用二進位法來放, 所以8個bit表示如下:</p>
<p>7-6-5-4-3-2-1-0</p>
<p>其中第0個bit最右側, 也就是最低位元, 最左邊是第7個bit, 也就是最高位元.</p>
<p>接下來要說明數字囉</p>
<p>00000001 (b)=1(d)</p>
<p>00000010(b)=2(d)</p>
<p>00000100(b)=4(d)</p>
<p>00010101(b)=21(d)</p>
<p>&#8230;. 以此類推, 而最大正數可以用這個表示, 除了第7個bit不動, 其餘都是1的話:</p>
<p>01111111(b)=127(d)</p>
<p>好, 接下來看負數怎麼表示. 先來看所謂二的補數, <strong>二的補數(two&#8217;s complement)就是將位元反置後再加一</strong>, 來看一個例子:</p>
<p>25(d)=00011001(b)</p>
<p>而要代表-25的二進位數, 則用 00011001(b)反置後再加一</p>
<p>1. 反置 11100110</p>
<p>2. 再加1 得 11100111,</p>
<p>3. 所以得 11100111(b)=-25(d)</p>
<p>再舉一個例子, 2(d)=00000010(b),</p>
<p>1. 反置 11111101,</p>
<p>2. 再加1得 11111110,</p>
<p>3. 所以得11111110(b)=-2(d)</p>
<p>接下來看如何反過來做, 若今天有個值是 110001010(b) 怎麼算回來呢?</p>
<p>1. 先減1, 110001001</p>
<p>2. 再反置得 001110110</p>
<p>3. 所以得 001110110(b)=118(d)</p>
<p>4. 所以 110001010(b)=-118(d)</p>
<p>這樣就行囉!</p>
<p>所以 8個bit的範圍最小到最大就是 -128~127共256個數字.</p>
<p>參考資料:</p>
<p><a href="http://blog.udn.com/Piner/3033066" target="_blank">http://blog.udn.com/Piner/3033066</a></p>
<p><a href="http://squall.cs.ntou.edu.tw/cprog/Materials/TwosComplementFormat.html" target="_blank">http://squall.cs.ntou.edu.tw/cprog/Materials/TwosComplementFormat.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://tim.diary.tw/2009/08/24/twos-complement/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

