-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.xml
3740 lines (3476 loc) · 469 KB
/
search.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>github+hexo架設心得</title>
<url>/2015/10/05/github-hexo%E6%9E%B6%E8%A8%AD%E5%BF%83%E5%BE%97/</url>
<content><![CDATA[<p>其實很早就想將自己的學習心得放在網路上給高中的學弟妹看,不然大學畢業後就沒空教學弟妹了</p>
<p>這篇是紀錄我如何架設blog的過程,除了跟風(github.io的域名),也是要給學弟妹的第一份筆記</p>
<h1 id="一、本機端安裝"><a href="#一、本機端安裝" class="headerlink" title="一、本機端安裝"></a>一、本機端安裝</h1><p>首先要先安裝<a href="https://nodejs.org/en/">node.js</a></p>
<p>最新版的node.js會自動加入環境變數,所以環境變數這邊就省了</p>
<p>接下來打開可愛的小黑窗安裝<a href="https://hexo.io/">hexo</a></p>
<pre><code>npm install -g hexo</code></pre>
<p>這樣hexo就安裝完成了</p>
<h1 id="二、github設定"><a href="#二、github設定" class="headerlink" title="二、github設定"></a>二、github設定</h1><p>到github開新的repository,命名為:<strong>用戶名.github.io</strong></p>
<h1 id="三、Hexo設定"><a href="#三、Hexo設定" class="headerlink" title="三、Hexo設定"></a>三、Hexo設定</h1><p>建立新的資料夾並且初始化</p>
<pre><code>mkdir hexo
hexo init</code></pre>
<p>產生頁面</p>
<pre><code>hexo generate</code></pre>
<p>啟動server(預設為localhost:4000)</p>
<pre><code>hexo server</code></pre>
<p>可以使用**-p**來調整port</p>
<pre><code>hexo server -p 8080</code></pre>
<h1 id="四、部署到github上面"><a href="#四、部署到github上面" class="headerlink" title="四、部署到github上面"></a>四、部署到github上面</h1><p>修改<strong>hexo</strong>資料夾底下的<strong>_config.yml</strong></p>
<p>到最底下修改</p>
<pre><code>deploy:
type: git
repository: [email protected]:github帳號/repository名稱.github.io.git
branch: master</code></pre>
<p>記得要先建立SSH的Key喔,可以參考<a href="https://help.github.com/articles/generating-ssh-keys/">官方文件</a></p>
<p>最後輸入</p>
<pre><code>hexo clean
hexo generate
hexo deploy</code></pre>
<p>接著到<strong>http://{github帳號}.github.io</strong>就可以看到成果囉</p>
<h1 id="心得"><a href="#心得" class="headerlink" title="心得"></a>心得</h1><p>Well,用Markdown寫文章寫起來真的很爽,之後來找插件加強網站的功能囉</p>
<h1 id="參考"><a href="#參考" class="headerlink" title="參考"></a>參考</h1><p>1.<a href="http://code.kpman.cc/2013/04/26/hexo%E6%9E%B6blog%E5%88%9D%E9%AB%94%E9%A9%97/">hexo架blog初體驗</a> </p>
<p>2.<a href="https://help.github.com/articles/generating-ssh-keys/">Generating SSH keys</a></p>
]]></content>
<categories>
<category>Hexo</category>
</categories>
<tags>
<tag>Hexo</tag>
<tag>Github</tag>
</tags>
</entry>
<entry>
<title>hexo留言與主題設定</title>
<url>/2015/10/06/hexo%E7%95%99%E8%A8%80%E8%88%87%E4%B8%BB%E9%A1%8C%E8%A8%AD%E5%AE%9A/</url>
<content><![CDATA[<p>覺得官方預設的主題有點不符合需求,只好找其他主題來玩</p>
<p>這邊還是要記錄一下換主題還有新增留言版過程</p>
<h1 id="更換主題"><a href="#更換主題" class="headerlink" title="更換主題"></a>更換主題</h1><p>到可以到<a href="https://github.com/hexojs/hexo/wiki/Themes">這裡</a>找你喜歡的主題</p>
<p>然後使用git安裝主題</p>
<pre><code>git clone <repository> themes/<theme-name></code></pre>
<p>然後修改<strong>_config.yml</strong></p>
<pre><code># Extensions
theme: <theme-name></code></pre>
<p>如果覺得這個主題有些小地方不滿意</p>
<p>可以到theme資料夾裡面修改<strong>_config.yml</strong></p>
<h2 id="主題設定"><a href="#主題設定" class="headerlink" title="主題設定"></a>主題設定</h2><p>以下都在_config.yml修改</p>
<h3 id="大頭貼設定"><a href="#大頭貼設定" class="headerlink" title="大頭貼設定"></a>大頭貼設定</h3><p>新增avatar</p>
<ol>
<li>完整的URL:<a href="http://gundambox.github.io/avatar/avatar.jpg">http://gundambox.github.io/avatar/avatar.jpg</a></li>
<li>站內地址:/avatar/avatar.jpg(在source/avatar目錄下)</li>
</ol>
<p>例:avatar: /avatar/avatar.jpg</p>
<h3 id="菜單設定"><a href="#菜單設定" class="headerlink" title="菜單設定"></a>菜單設定</h3><p>在menu裡面可以設定</p>
<table>
<thead>
<tr>
<th>Key</th>
<th>Value</th>
<th>Comment</th>
</tr>
</thead>
<tbody><tr>
<td>home</td>
<td>home: /</td>
<td>首頁</td>
</tr>
<tr>
<td>archives</td>
<td>archives: /archives</td>
<td>歸檔</td>
</tr>
<tr>
<td>categories</td>
<td>categories: /categories</td>
<td>分類(需手動建立)</td>
</tr>
<tr>
<td>tags</td>
<td>tags: /tags</td>
<td>標籤(需手動建立)</td>
</tr>
<tr>
<td>about</td>
<td>about: /about</td>
<td>關於(需手動建立)</td>
</tr>
<tr>
<td>commonweal</td>
<td>commonweal: /404.html</td>
<td>404頁面(需手動建立)</td>
</tr>
</tbody></table>
<p>菜單範例</p>
<pre><code>menu:
home: /
categories: /categories
about: /about
archives: /archives
tags: /tags
#commonweal: /404.html</code></pre>
<h3 id="標籤頁面"><a href="#標籤頁面" class="headerlink" title="標籤頁面"></a>標籤頁面</h3><ol>
<li><p>新建頁面</p>
<p> hexo new page “tags”</p>
</li>
<li><p>編輯頁面</p>
</li>
</ol>
<p>編輯剛剛產生的頁面,將類型設為tags</p>
<pre><code>title: index
date: 2014-12-22 12:39:04
type: "tags"
---</code></pre>
<p>像我有使用Disqus功能,預設頁面也會評論,如果不想要的話</p>
<p>要加入comments: false</p>
<pre><code>title: index
date: 2014-12-22 12:39:04
type: "tags"
comments: false
---</code></pre>
<ol start="3">
<li>加入頁面</li>
</ol>
<p>最後編輯_config.yml,添加tags到menu底下</p>
<pre><code>menu:
home: /
archives: /archives
tags: /tags</code></pre>
<h3 id="分類頁面"><a href="#分類頁面" class="headerlink" title="分類頁面"></a>分類頁面</h3><ol>
<li><p>新建頁面</p>
<p> hexo new page “categories”</p>
</li>
<li><p>編輯頁面</p>
</li>
</ol>
<p>編輯剛剛產生的頁面,將類型設為tags</p>
<pre><code>title: index
date: 2014-12-22 12:39:04
type: "categories"
---</code></pre>
<p>像我有使用Disqus功能,預設頁面也會評論,如果不想要的話</p>
<p>要加入comments: false</p>
<pre><code>title: index
date: 2014-12-22 12:39:04
type: "categories"
comments: false
---</code></pre>
<ol start="3">
<li>加入頁面</li>
</ol>
<p>最後編輯_config.yml,添加categories到menu底下</p>
<pre><code>menu:
home: /
categories: /categories
archives: /archives
tags: /tags</code></pre>
<h3 id="關於頁面"><a href="#關於頁面" class="headerlink" title="關於頁面"></a>關於頁面</h3><ol>
<li><p>新建頁面</p>
<p> hexo new page “about”</p>
</li>
<li><p>加入頁面</p>
</li>
</ol>
<p>最後編輯_config.yml,添加categories到menu底下</p>
<pre><code>menu:
home: /
archives: /archives
tags: /tags
about: /about</code></pre>
<h3 id="側邊欄社交連結"><a href="#側邊欄社交連結" class="headerlink" title="側邊欄社交連結"></a>側邊欄社交連結</h3><p>新增social,然後添加站點與地址即可</p>
<pre><code># Social links
social:
github: https://github.com/your-user-name
twitter: https://twitter.com/your-user-name
weibo: http://weibo.com/your-user-name
douban: http://douban.com/people/your-user-name
zhihu: http://www.zhihu.com/people/your-user-name</code></pre>
<h1 id="新增留言板"><a href="#新增留言板" class="headerlink" title="新增留言板"></a>新增留言板</h1><p>首先先到<a href="https://disqus.com/">Disqus</a>官方網站註冊帳號</p>
<p>然後新增頁面</p>
<p><img src="/2015/10/06/hexo%E7%95%99%E8%A8%80%E8%88%87%E4%B8%BB%E9%A1%8C%E8%A8%AD%E5%AE%9A/hexo%E7%95%99%E8%A8%80%E8%88%87%E4%B8%BB%E9%A1%8C%E8%A8%AD%E5%AE%9A-1.PNG" alt="新增頁面1"></p>
<p><img src="/2015/10/06/hexo%E7%95%99%E8%A8%80%E8%88%87%E4%B8%BB%E9%A1%8C%E8%A8%AD%E5%AE%9A/hexo%E7%95%99%E8%A8%80%E8%88%87%E4%B8%BB%E9%A1%8C%E8%A8%AD%E5%AE%9A-2.PNG" alt="新增頁面2"></p>
<p><img src="/2015/10/06/hexo%E7%95%99%E8%A8%80%E8%88%87%E4%B8%BB%E9%A1%8C%E8%A8%AD%E5%AE%9A/hexo%E7%95%99%E8%A8%80%E8%88%87%E4%B8%BB%E9%A1%8C%E8%A8%AD%E5%AE%9A-3.PNG" alt="新增頁面3"></p>
<p><img src="/2015/10/06/hexo%E7%95%99%E8%A8%80%E8%88%87%E4%B8%BB%E9%A1%8C%E8%A8%AD%E5%AE%9A/hexo%E7%95%99%E8%A8%80%E8%88%87%E4%B8%BB%E9%A1%8C%E8%A8%AD%E5%AE%9A-4.PNG" alt="記下shortname"></p>
<p>最後到<strong>_config.yml</strong>加入</p>
<pre><code># Disqus
disqus_shortname: gundambox</code></pre>
<p>就大功告成囉</p>
<h1 id="參考"><a href="#參考" class="headerlink" title="參考"></a>參考</h1><ol>
<li><p><a href="http://leapwing.github.io/2015/04/23/hexo-setting/">Hexo相關設定</a></p>
</li>
<li><p><a href="http://bbandydd.github.io/hexoBlog/2015/05/13/Hexo-add-Comments/">Hexo 留言板 - Disqus</a></p>
</li>
<li><p><a href="http://theme-next.iissnan.com/five-minutes-setup.html">5 分钟快速安装</a></p>
</li>
<li><p><a href="http://theme-next.iissnan.com/theme-settings.html">主题设定</a></p>
</li>
</ol>
]]></content>
<tags>
<tag>Hexo</tag>
</tags>
</entry>
<entry>
<title>安裝數學公式插件</title>
<url>/2015/10/07/%E5%AE%89%E8%A3%9D%E6%95%B8%E5%AD%B8%E5%85%AC%E5%BC%8F%E6%8F%92%E4%BB%B6/</url>
<content><![CDATA[<p>因為之後可能會用到數學公式,所以就先來安裝囉</p>
<h1 id="安裝"><a href="#安裝" class="headerlink" title="安裝"></a>安裝</h1><p>首先是安裝</p>
<pre><code>npm install hexo-math --save</code></pre>
<p>然後在<strong>_config.yml</strong>中加入</p>
<pre><code>plugins:
- hexo-math</code></pre>
<p>話說新版的不需要執行下面這行指令</p>
<pre><code>hexo math install</code></pre>
<p>接下來就來試試看效果如何</p>
<h1 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h1><h2 id="單行"><a href="#單行" class="headerlink" title="單行"></a>單行</h2><pre><code>$a = b + c$</code></pre>
<p>效果:</p>
<p>$a = b + c$</p>
<h2 id="多行"><a href="#多行" class="headerlink" title="多行"></a>多行</h2><pre><code>$$\frac{\partial u}{\partial t}
= h^2 \left( \frac{\partial^2 u}{\partial x^2} +
\frac{\partial^2 u}{\partial y^2} +
\frac{\partial^2 u}{\partial z^2}\right)$$</code></pre>
<p>效果:</p>
<p>$$\frac{\partial u}{\partial t}<br>= h^2 \left( \frac{\partial^2 u}{\partial x^2} +<br>\frac{\partial^2 u}{\partial y^2} +<br>\frac{\partial^2 u}{\partial z^2}\right)$$</p>
<h1 id="參考"><a href="#參考" class="headerlink" title="參考"></a>參考</h1><p>1.<a href="http://catx.me/2014/03/09/hexo-mathjax-plugin/">Hexo MathJax插件</a></p>
<p>2.<a href="http://hijiangtao.github.io/2014/09/08/MathJaxinHexo/">Hexo上使用MathJax来实现数学公式的表达</a></p>
<p>3.<a href="http://iori.sinaapp.com/17.html/comment-page-1?replytocom=2">MathJax使用LaTeX语法编写数学公式教程</a></p>
]]></content>
<categories>
<category>Hexo</category>
</categories>
<tags>
<tag>Hexo</tag>
</tags>
</entry>
<entry>
<title>計算機概論-數字系統</title>
<url>/2015/10/08/%E8%A8%88%E7%AE%97%E6%A9%9F%E6%A6%82%E8%AB%96-%E6%95%B8%E5%AD%97%E7%B3%BB%E7%B5%B1/</url>
<content><![CDATA[<p>在寫C語言筆記之前,我覺得要先建立一些數字的概念</p>
<p>所以我就挑了大學時候的<a href="http://www.books.com.tw/products/F011353305">計算機概論</a>,擷取一些我覺得重要的地方來講囉</p>
<p>之後會按照順序講</p>
<ul>
<li>數字系統</li>
<li>資料儲存</li>
<li>邏輯運算</li>
</ul>
<p>這篇是講關於數字系統之間的轉換</p>
<h1 id="定義"><a href="#定義" class="headerlink" title="定義"></a>定義</h1><p>這邊只講進位制(positional number system)</p>
<p>一個數字表示為 $ \pm(S_{k-1} \cdots S_{0}.S_{-1} \cdots S_{-l})_{b} $</p>
<p>表示值為 $ N = \pm(S_{k-1}*b^{k-1}+\cdots+S_0*b^0+S_{-1}*b^{-1}+\cdots+S_{-l}*b^{-l}) $</p>
<p>其中<strong>S</strong>表示為數字的集合,<strong>b</strong>是基底(base or radix)</p>
<h1 id="十進位系統"><a href="#十進位系統" class="headerlink" title="十進位系統"></a>十進位系統</h1><p>十進位系統是以10為基底,由0、1、2、3、4、5、6、7、8、9等十個數字組成,逢10就進位</p>
<p>寫法為:$ (5487)_{10} $,但一般會省略基底表示為:$ 5487 $</p>
<h2 id="整數"><a href="#整數" class="headerlink" title="整數"></a>整數</h2><p>十進位整數 $ N $ 可以表示為 $ \pm(S_{k-1}*10^{k-1}+\cdots+S_0*10^0) $</p>
<p>例如 $ 5487 $ 可以表示為 $ 5*10^3+4*10^2+8*10^1+7*10^0 $</p>
<h2 id="浮點數"><a href="#浮點數" class="headerlink" title="浮點數"></a>浮點數</h2><p>十進位浮點數 $ R $ </p>
<p>可以表示為 $ \pm(\overbrace{S_{k-1}*10^{k-1}+\cdots+S_0*10^0}^{Integer\ part}+\overbrace{S_{-1}*10^{-1}+\cdots+S_{-l}*10^{-l}}^{fractional\ part}) $</p>
<p>例如 $ 42689.2 $ 可以表示為 $ 4*10^4+2*10^3+6*10^2+8*10^1+9*10^0+2*10^{-1} $</p>
<h1 id="二進位系統"><a href="#二進位系統" class="headerlink" title="二進位系統"></a>二進位系統</h1><p>二進位系統是以2為基底,由0、1所組成,逢2進位</p>
<p>寫法為$ (101)_2 $</p>
<h2 id="整數-1"><a href="#整數-1" class="headerlink" title="整數"></a>整數</h2><p>二進位整數 $ N $ 可以表示為 $ \pm(S_{k-1}*2^{k-1}+\cdots+S_0*2^0) $</p>
<p>例如 $ (1011)_2 $ 可以表示為 $ 1*2^3+0*2^2+1*2^1+1*2^0 $</p>
<h2 id="浮點數-1"><a href="#浮點數-1" class="headerlink" title="浮點數"></a>浮點數</h2><p>二進位浮點數 $ R $ 可以表示為 $ \pm(\overbrace{S_{k-1}*2^{k-1}+\cdots+S_0*2^0}^{Integer\ part}+\overbrace{S_{-1}*2^{-1}+\cdots+S_{-l}*2^{-l}}^{fractional\ part}) $</p>
<p>例如 $ (101.01)_2 $ 可以表示為 $ 1*2^3+0*2^2+1*2^0+0*2^{-1}+1*2^{-2} $</p>
<h1 id="八進位系統"><a href="#八進位系統" class="headerlink" title="八進位系統"></a>八進位系統</h1><p>八進位系統是以8為基底,由0、1、2、3、4、5、6、7、所組成,逢8進位</p>
<p>寫法為$ (755)_8 $,也可以寫成$ 0755 $</p>
<h2 id="整數-2"><a href="#整數-2" class="headerlink" title="整數"></a>整數</h2><p>八進位整數 $ N $ 可以表示為 $ \pm(S_{k-1}*8^{k-1}+\cdots+S_0*8^0) $</p>
<p>例如 $ (1234)_8 $ 可以表示為 $ 1*8^3+2*8^2+3*8^1+4*8^0 $</p>
<h2 id="浮點數-2"><a href="#浮點數-2" class="headerlink" title="浮點數"></a>浮點數</h2><p>八進位浮點數 $ R $ 可以表示為 $ \pm(\overbrace{S_{k-1}*8^{k-1}+\cdots+S_0*8^0}^{Integer\ part}+\overbrace{S_{-1}*8^{-1}+\cdots+S_{-l}*8^{-l}}^{fractional\ part}) $</p>
<p>例如 $ (765.67)_8 $ 可以表示為 $ 7*8^3+6*8^2+5*8^0+6*8^{-1}+7*8^{-2} $</p>
<h1 id="十六進位系統"><a href="#十六進位系統" class="headerlink" title="十六進位系統"></a>十六進位系統</h1><p>十六進位系統是以16為基底</p>
<p>由0、1、2、3、4、5、6、7、8、9、A(10)、B(11)、C(12)、D(13)、E(14)、F(15)所組成,逢16進位</p>
<p>寫法為$ (FF)_{16} $,也可以寫成$ 0xFF $</p>
<h2 id="整數-3"><a href="#整數-3" class="headerlink" title="整數"></a>整數</h2><p>十六進位整數 $ N $ 可以表示為 $ \pm(S_{k-1}*16^{k-1}+\cdots+S_0*16^0) $</p>
<p>例如 $ (FE98)_{16} $ 可以表示為 $ 15*16^3+14*16^2+9*16^1+8*16^0 $</p>
<h2 id="浮點數-3"><a href="#浮點數-3" class="headerlink" title="浮點數"></a>浮點數</h2><p>十六進位浮點數 $ R $ 可以表示為 </p>
<p>$ \pm(\overbrace{S_{k-1}*16^{k-1}+\cdots+S_0*16^0}^{Integer\ part}+\overbrace{S_{-1}*16^{-1}+\cdots+S_{-l}*16^{-l}}^{fractional\ part}) $</p>
<p>例如 $ (ABC.DEF)_{16} $ 可以表示為 </p>
<p>$ 10*16^3+11*16^2+12*16^0+13*16^{-1}+14*16^{-2}+15*16{-3} $</p>
<h1 id="進制轉換"><a href="#進制轉換" class="headerlink" title="進制轉換"></a>進制轉換</h1><h2 id="其他進位轉十進位"><a href="#其他進位轉十進位" class="headerlink" title="其他進位轉十進位"></a>其他進位轉十進位</h2><p>任何進位要轉成十進位很簡單,轉換方法為</p>
<p>$ N = S_{k-1}*b^{k-1}+\cdots+S_0*b^0+S_{-1}*b^{-1}+\cdots+S_{-l}*b^{-l} $ </p>
<p>例1<br>$$ \begin{split}<br>&(110.11)_{2} \\<br>&= 1*2^2+1*2^1+0*2^0+1*2^{-1}+1*2^{-2} \\<br>&= 4+2+0+0.5+0.25 = 0.75<br>\end{split} $$</p>
<p>例2<br>$$ \begin{split}<br>&(1A.23)_{16} \\<br>&= 1*16^1+10*16^0+2*16^{-1}+3*16^{-2} \\<br>&= 16+10+0.125+0.012 = 26.137 \\<br>\end{split} $$</p>
<h2 id="十進位轉其他進位"><a href="#十進位轉其他進位" class="headerlink" title="十進位轉其他進位"></a>十進位轉其他進位</h2><h3 id="整數部分"><a href="#整數部分" class="headerlink" title="整數部分"></a>整數部分</h3><ol>
<li>轉二進位</li>
</ol>
<p>$$ \begin{split}<br>2|\underline{35}& \rightarrow 餘數 1 \\<br>2|\underline{17}& \rightarrow 餘數 1 \\<br>2|\underline{8}& \rightarrow 餘數 0 \\<br>2|\underline{4}& \rightarrow 餘數 0 \\<br>2|\underline{2}& \rightarrow 餘數 0 \\<br>1& \rightarrow 餘數 1<br>\end{split} $$</p>
<p>然後由下到上取出餘數,也就是100011,就可以得到$ (35)_{10} = (100011)_{2} $了 </p>
<ol start="2">
<li>轉八進位<br>$$ \begin{split}<br>8|\underline{126}& \rightarrow 餘數 6 $ \\<br>8|\underline{15}& \rightarrow 餘數 7 $ \\<br>1& \rightarrow 餘數 1<br>\end{split} $$</li>
</ol>
<p>然後由下到上取出餘數,也就是176,就可以得到$ (126)_{10} = (176)_{8} $了 </p>
<ol start="3">
<li>轉十六進位<br>$$ \begin{split}<br>16|\underline{126}& \rightarrow 餘數 E(14) \\<br>16|\underline{7}& \rightarrow 餘數 7 \\<br>0&<br>\end{split} $$</li>
</ol>
<p>然後由下到上取出餘數,也就是E7,就可以得到$ (126)_{10} = (E7)_{16} $了 </p>
<h3 id="浮點數部分"><a href="#浮點數部分" class="headerlink" title="浮點數部分"></a>浮點數部分</h3><ol>
<li>轉二進位<br>$$ \begin{split}<br>&0.625 \\<br>*&\underline{\ \ \ \ \ \ \ 2} \\<br>&1.250 \rightarrow 取出1 \\<br>&0.250 \\<br>*&\underline{\ \ \ \ \ \ \ 2} \\<br>&0.500 \rightarrow 取出0 \\<br>*&\underline{\ \ \ \ \ \ \ 2} \\<br>&1.000 \rightarrow 取出1<br>\end{split} $$</li>
</ol>
<p>然後由上到下取出數字,也就是101,就可以得到$ (0.625)_{10} = (0.101)_2 $了</p>
<ol start="2">
<li>轉八進位<br>$$ \begin{split}<br>&0.171875 \\<br>*&\underline{\ \ \ \ \ \ \ \ \ \ \ \ \ 8} \\<br>&1.375000 \rightarrow 取出1 \\<br>&0.375000 \\<br>*&\underline{\ \ \ \ \ \ \ \ \ \ \ \ \ 8} \\<br>&3.000000 \rightarrow 取出3 \\<br>\end{split} $$</li>
</ol>
<p>然後由上到下取出數字,也就是13,就可以得到$ (0.171875)_{10} = (0.13)_8 $了</p>
<ol start="3">
<li>轉十六進位<br>$$ \begin{split}<br>&\ \ 0.89453125 \\<br>*&\underline{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 16} \\<br>&14.31250000 \rightarrow 取出E(14) \\<br>&0.312500000 \\<br>*&\underline{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 16} \\<br>&5.000000000 \rightarrow 取出5 \\<br>\end{split} $$</li>
</ol>
<p>然後由上到下取出數字,也就是E5,就可以得到$ (0.89453125)_{10} = (0.E5)_16 $了</p>
<p><strong>小數從十進位轉到其他進位有可能會產生無限循環的小數,例如</strong> $ (0.1)_{10} = (0.1\overline{99})_{16} $</p>
<h2 id="二進位轉十六進位"><a href="#二進位轉十六進位" class="headerlink" title="二進位轉十六進位"></a>二進位轉十六進位</h2><h3 id="整數-4"><a href="#整數-4" class="headerlink" title="整數"></a>整數</h3><p>由右往左,每四個位元轉成十六進位,如果位數不是四的倍數,則在數字前面補零</p>
<p>例如$ (1010101010)_2 $:$ (\overbrace{0010}^{2}\overbrace{1010}^{A}\overbrace{1010}^{A})_2 $</p>
<p>反過來就是將每位數字從十六進位轉為二進位,然後將結果連接起來</p>
<p>例如$ (12)_{16} $:$ (\overbrace{1}^{0001}\overbrace{2}^{0010})_{16} $</p>
<h3 id="浮點數-4"><a href="#浮點數-4" class="headerlink" title="浮點數"></a>浮點數</h3><p>由左往右,每四個位元轉成十六進位,如果位數不是四的倍數,則在數字後面補零</p>
<p>例如$ (0.01111)_2 $:$ (0.\overbrace{0111}^{7}\overbrace{1000}^{8})_2 $</p>
<p>反過來就是將每位數字從十六進位轉為二進位,然後將結果連接起來</p>
<p>例如$ (0.5A)_{16} $:$ (\overbrace{5}^{0101}\overbrace{A}^{1010})_{16} $</p>
<h2 id="二進位轉八進位"><a href="#二進位轉八進位" class="headerlink" title="二進位轉八進位"></a>二進位轉八進位</h2><h3 id="整數-5"><a href="#整數-5" class="headerlink" title="整數"></a>整數</h3><p>由右往左,每三個位元轉成八進位,如果位數不是三的倍數,則在數字前面補零</p>
<p>例如$ (1010101)_2 $:$ (\overbrace{001}^{1}\overbrace{010}^{2}\overbrace{101}^{5})_8 $</p>
<p>反過來就是將每位數字從八進位轉為二進位,然後將結果連接起來</p>
<p>例如$ (34)_{8} $:$ (\overbrace{3}^{011}\overbrace{4}^{100})_{8} $</p>
<h3 id="浮點數-5"><a href="#浮點數-5" class="headerlink" title="浮點數"></a>浮點數</h3><p>由左往右,每三個位元轉成八進位,如果位數不是三的倍數,則在數字後面補零</p>
<p>例如$ (0.0111)_2 $:$ (0.\overbrace{011}^{3}\overbrace{100}^{4})_2 $</p>
<p>反過來就是將每位數字從十六進位轉為二進位,然後將結果連接起來</p>
<p>例如$ (0.57)_{8} $:$ (\overbrace{5}^{101}\overbrace{7}^{111})_{8} $</p>
<h2 id="八進位轉十六進位"><a href="#八進位轉十六進位" class="headerlink" title="八進位轉十六進位"></a>八進位轉十六進位</h2><p>其實就把八進位轉成二進位再轉成十六進位而已XD</p>
]]></content>
<categories>
<category>計算機概論</category>
</categories>
<tags>
<tag>計算機概論</tag>
</tags>
</entry>
<entry>
<title>計算機概論-資料儲存</title>
<url>/2015/10/13/%E8%A8%88%E7%AE%97%E6%A9%9F%E6%A6%82%E8%AB%96-%E8%B3%87%E6%96%99%E5%84%B2%E5%AD%98/</url>
<content><![CDATA[<p>講完了數字系統,這一篇是講關於資料儲存,我挑了數字來講,其他的我覺得書本看過去就好。</p>
<h1 id="在電腦裡面的資料"><a href="#在電腦裡面的資料" class="headerlink" title="在電腦裡面的資料"></a>在電腦裡面的資料</h1><p>位元(bit)是電腦儲存資料的最小單位,數值為0或1</p>
<p>每八個位元為一個位元組(byte)</p>
<h1 id="儲存數字"><a href="#儲存數字" class="headerlink" title="儲存數字"></a>儲存數字</h1><h2 id="整數"><a href="#整數" class="headerlink" title="整數"></a>整數</h2><h3 id="無號整數"><a href="#無號整數" class="headerlink" title="無號整數"></a>無號整數</h3><p>無號整數是大於等於零的整數,範圍是零到無限大</p>
<p>但是電腦的儲存空間有限,所以數字會有表達範圍</p>
<p>假如電腦能儲存的空間有n位元,那就表示電腦能表達的最大數字為 $ 2^n-1 $</p>
<p>那麼在電腦中是如何儲存n位元的無號整數的呢</p>
<ul>
<li>將數字轉為二進位</li>
<li>如果不滿n位元,則在前面補零</li>
</ul>
<p>例如:將7儲存為8位元的無號整數</p>
<ul>
<li>轉為二進位為 $ (111)_2 $</li>
<li>在前面補零到八個位元為止 $ (00000111)_2 $</li>
</ul>
<p>如果將兩個整數相加但相加結果超出n位元的表達範圍(邊界),稱之為<strong>overflow</strong></p>
<p>例如在8位元下</p>
<p>$ (11111111)_2+(00000001)_2 $ 應該等於 $ (100000000)_2 $</p>
<p>但是空間只有8位元的情況下</p>
<p>第九個位元會被捨去</p>
<p>就會使結果變成 $ (00000000)_2 $</p>
<h3 id="有號整數-sign-and-magnitude"><a href="#有號整數-sign-and-magnitude" class="headerlink" title="有號整數(sign-and-magnitude)"></a>有號整數(sign-and-magnitude)</h3><p>那怎麼表達正負號呢</p>
<p>於是就定義了最前面(最左邊)的位元來表示正負號</p>
<p>0表示正號,1表示負號</p>
<p>雖然在n位元下,能表達的數字範圍少了一半,但總算能表達正負號了</p>
<p>例1:28存為8位元有號整數時</p>
<ul>
<li>先轉為二進位 $ (11100)_2 $</li>
<li>補零補到七位元 $ (0011100)_2 $</li>
<li>視正負號補上第八位元 $ (00011100)_2 $</li>
</ul>
<p>例2:-28存為8位元有號整數時</p>
<ul>
<li>先轉為二進位 $ (11100)_2 $</li>
<li>補零補到七位元 $ (0011100)_2 $</li>
<li>視正負號補上第八位元 $ (10011100)_2 $</li>
</ul>
<p>在這邊overflow仍然是存在的</p>
<p><strong>注意</strong>:這種做法會有正零跟負零喔出現喔</p>
<h3 id="二補數"><a href="#二補數" class="headerlink" title="二補數"></a>二補數</h3><p>雖然解決了表達正負號的問題,但電路上的實作卻遇到一個問題</p>
<p>加法跟減法需要分開實作,於是就有人想了,能不能兼具實作方便又能表達正負數</p>
<p>於是就有了二補數,做法為:</p>
<ul>
<li>將數字做一補數</li>
<li>將數字加1</li>
</ul>
<p>何謂一補數呢?只要將原數字的二進位反轉就是一補數了</p>
<p>例如8位元下, $ (00000101)_2 $ 的一補數是 $ (11111010)_2 $</p>
<p>而 $ (5)_{10} = (00000101)_2 $ 的二補數就是 $ (11111010)_2 $ 再加1,變成 $ (-5)_{10} = (11111011)_2 $</p>
<p>到底為什麼二補數可以讓減法用加法解決呢</p>
<p>例如 $ 1-1=0 $,我們可以將減法當作加上一個負值,變成 $ 1+(-1)=0 $</p>
<p>來檢驗一下這條算式是不是對的</p>
<p>1為 $ (00000001)_2 $,-1為 $ (11111111)_2 $</p>
<p>兩個相加等於 $ (100000000)_2 $,但是第九個位元會被捨去</p>
<p>所以就變成了 $ (00000000)_2 $</p>
<p>又例如 $ 5-4=5+(-4)=(00000101)_2+(11111100)_2=(100000001)_2 $</p>
<p>去掉第九位元之後就變成 $ (00000001)_2=(1)_{10} $</p>
<h2 id="浮點數"><a href="#浮點數" class="headerlink" title="浮點數"></a>浮點數</h2><h3 id="正規化與IEEE標準"><a href="#正規化與IEEE標準" class="headerlink" title="正規化與IEEE標準"></a>正規化與IEEE標準</h3><p>在儲存浮點數前,要先將二進位數字化為適當的科學記號,也就是正規化,然後按照IEEE訂的標準儲存</p>
<p>正規化就是將數字轉為 $ 1.xxxxx*2^{exp} $ 的形式</p>
<p>例如 $ (0.000000101001)_2 $ 要化為 $ 1.01001*2^7 $</p>
<p>然後我們可以得到三個部分</p>
<ul>
<li>Sign (正負號)</li>
<li>Exponent (指數)</li>
<li>Mantissa (尾數)</li>
</ul>
<p>舉 $ 1.01001*2^7 $ 為例,Sign就是+,Exponent為7,Mantissa為01001</p>
<p>取出這三個部分後,就可以按照IEEE標準儲存浮點數了</p>
<p>IEEE規定32位元浮點數的Sign為1位元,Exponent為8位元,Mantissa為23位元</p>
<p>64位元浮點數的Sign為1位元,Exponent為11位元,Mantissa為52位元</p>
<p>其中Exponent不是用二補數儲存,而是採用無號整數</p>
<p>負數均位移 $ 2^{Exponent-1}-1 $,也就是加 $ 2^{Exponent-1}-1 $</p>
<p>例如1位移之後變128,-1位移之後變126</p>
<p>儲存浮點數的步驟:</p>
<ul>
<li>確認正負號 (0 or 1)</li>
<li>將數字轉成二進位</li>
<li>正規化</li>
<li>找出Exponent與Mantissa</li>
<li>將結果連接起來</li>
</ul>
<p>例如-0.0234375:</p>
<ul>
<li>確認正負號:1</li>
<li>將數字轉為二進位:$ (0.0000011)_2 $</li>
<li>正規化:$ (1.1)_2*2^{-6} $ </li>
<li>找出Exponent與Mantissa:$ E=-6+127=121=(01111001)_2 $ 且 $ M=(1)_2 $</li>
<li>將結果連接起來:$ \overbrace{1}^{Sign}\overbrace{01111001}^{Exponent}\overbrace{10000000000000000000000}^{Mantissa} $</li>
</ul>
<p>有一個特別情況是數字為0的時候</p>
<p>不是00111111100000000000000000000000</p>
<p>也不是10111111100000000000000000000000喔XD</p>
<p>IEEE有特別規定所有位元為0的時候表示數字為0</p>
<h3 id="overflow-and-underflow"><a href="#overflow-and-underflow" class="headerlink" title="overflow and underflow"></a>overflow and underflow</h3><p>overflow之前就談過了</p>
<p>如果你試圖存取超過 $ \pm(1.11111111111111111111111)_2*2^{128} $ 的數字</p>
<p>記憶體就會炸裂給你看www</p>
<p>underflow是指試圖存取比範圍更小的數字</p>
<p>也就是 $ \pm(0.00000000000000000000001)_2*2^{-127} $</p>
<h3 id="截斷誤差"><a href="#截斷誤差" class="headerlink" title="截斷誤差"></a>截斷誤差</h3><p>如果你試圖將一個極大一個極小的數字相加就會產生截斷誤差</p>
<p>例如 $ (11111111111111111111.11)_2+(0.00000000000000000000001)_2 $</p>
<p>結果應該是 $ (11111111111111111111.11000000000000000000001)_2 $</p>
<p>正規化後變成 $ (1.111111111111111111111000000000000000000001)_2*2^{19} $</p>
<p>但是在儲存的時候,尾數只有23位元</p>
<p>也就是11111111111111111111100會被儲存,最後的1卻不見了</p>
<p>所以在做浮點數運算的時候要非常小心</p>
]]></content>
<categories>
<category>計算機概論</category>
</categories>
<tags>
<tag>計算機概論</tag>
</tags>
</entry>
<entry>
<title>計算機概論-邏輯運算</title>
<url>/2015/10/15/%E8%A8%88%E7%AE%97%E6%A9%9F%E6%A6%82%E8%AB%96-%E9%82%8F%E8%BC%AF%E9%81%8B%E7%AE%97/</url>
<content><![CDATA[<p>這篇是講關於邏輯運算的一些觀念</p>
<h1 id="邏輯運算"><a href="#邏輯運算" class="headerlink" title="邏輯運算"></a>邏輯運算</h1><h2 id="NOT"><a href="#NOT" class="headerlink" title="NOT"></a>NOT</h2><p>NOT運算子是一元運算子,只有一個輸入,輸出與輸入相反的值</p>
<p>輸入0就會輸出1,輸入1就會輸出0</p>
<p>truth table:</p>
<table>
<thead>
<tr>
<th>x</th>
<th>NOT x</th>
</tr>
</thead>
<tbody><tr>
<td>0</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>0</td>
</tr>
</tbody></table>
<p>擴展到n bits之後就可以做一補數運算了www</p>
<p>例如:NOT 10011000 = 01100111</p>
<h2 id="AND"><a href="#AND" class="headerlink" title="AND"></a>AND</h2><p>AND運算子是二元運算子,有兩個輸入</p>
<p>只有兩個輸入同時為1的時候才會輸出1,其他輸出0</p>
<p>truth table:</p>
<table>
<thead>
<tr>
<th>x</th>
<th>y</th>
<th>x AND y</th>
</tr>
</thead>
<tbody><tr>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>1</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>0</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
</tbody></table>
<p>擴展到n bits之後可以用來做unset(強制設為0)</p>
<p>例如10100110 AND 00000111 = 00000110</p>
<h2 id="OR"><a href="#OR" class="headerlink" title="OR"></a>OR</h2><p>OR運算子是二元運算子,有兩個輸入</p>
<p>只要其中一個輸入為1就會輸出1,其他輸出0</p>
<p>truth table:</p>
<table>
<thead>
<tr>
<th>x</th>
<th>y</th>
<th>x OR y</th>
</tr>
</thead>
<tbody><tr>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>1</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<td>0</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
</tbody></table>
<p>擴展到n bits之後可以設定特定位元為1</p>
<p>例如10100110 OR 11111000 = 11111110</p>
<h2 id="XOR"><a href="#XOR" class="headerlink" title="XOR"></a>XOR</h2><p>XOR運算子是二元運算子(念作exclusive-or),有兩個輸入</p>
<p>兩個輸入不一樣的時候才會輸出1,其他輸出0</p>
<p>XOR其實是由前三個運算組合起來的</p>
<p>$ x\ XOR\ y\leftrightarrow[x\ AND\ (NOT\ y)]\ OR\ [(NOT\ x)\ AND\ y] $</p>
<p>truth table:</p>
<table>
<thead>
<tr>
<th>x</th>
<th>y</th>
<th>x XOR y</th>
</tr>
</thead>
<tbody><tr>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>1</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<td>0</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>0</td>
</tr>
</tbody></table>
<p>擴展到n bits之後可以用來反轉特定位元</p>
<p>例如10100110 OR 11111000 = 01011110</p>
<h1 id="位移運算"><a href="#位移運算" class="headerlink" title="位移運算"></a>位移運算</h1><h2 id="邏輯位移"><a href="#邏輯位移" class="headerlink" title="邏輯位移"></a>邏輯位移</h2><ul>
<li>右移</li>
</ul>
<p>所有位元都往右移一位,捨棄最右邊的位元,最左邊的位元填上0</p>
<p>例如10011000右移一位之後變成01001100</p>
<ul>
<li>左移</li>
</ul>
<p>所有位元都往左移一位,捨棄最左邊的位元,最右邊的位元填上0</p>
<p>例如10011000左移一位之後變成00110000</p>
<h2 id="循環位移"><a href="#循環位移" class="headerlink" title="循環位移"></a>循環位移</h2><ul>
<li>右移</li>
</ul>
<p>所有位元都往右移一位,最右邊的位元補到最左邊的位元</p>
<p>例如10011001右移一位之後變成11001100</p>
<ul>
<li>左移</li>
</ul>
<p>所有位元都往左移一位,捨棄最左邊的位元補到最右邊的位元</p>
<p>例如10011001左移一位之後變成00110011</p>
<h2 id="算術位移"><a href="#算術位移" class="headerlink" title="算術位移"></a>算術位移</h2><ul>
<li>右移</li>
</ul>
<p>所有位元都往右移一位,捨棄最右邊的位元,最左邊的位元填上原本的位元(保持原本的正負號)</p>
<p>例如10011000右移一位之後變成11001100</p>
<ul>
<li>左移</li>
</ul>
<p>所有位元都往左移一位,捨棄最左邊的位元,最右邊的位元填上0</p>
<p>例如10011000左移一位之後變成00110000</p>
<h1 id="算術運算"><a href="#算術運算" class="headerlink" title="算術運算"></a>算術運算</h1><h2 id="整數-二補數-的加減法"><a href="#整數-二補數-的加減法" class="headerlink" title="整數(二補數)的加減法"></a>整數(二補數)的加減法</h2><p>整數(二補數)運算的過程:</p>
<p>$ A \pm B=? $</p>
<ol>
<li>判斷是加法或減法</li>
<li>若為減法則做二補數</li>
<li>相加</li>
</ol>
<p>例1:A=$ (00010001)_2 $,B=$ (00010110)_2 $</p>
<p>A+B=?</p>
<ol>
<li>判斷是加法或減法:加法</li>
<li>若為減法則做二補數:跳過</li>
<li>相加<br>$$ \begin{split}<br>&\ \ \ \ 1\ \ \ \ \ \ \ \ \ \ \ \ Carry(進位) \\<br>&00010001\ A \\</li>
</ol>
<p>+&\underline{00010110}\ B \\<br>&00100111\ R(結果)<br>\end{split} $$</p>
<p>我們得到 (+17) + (+22) = (+39)</p>
<p>例2:A=$ (11011101)_2 $,B=$ (00010100)_2 $</p>
<p>A-B=?</p>
<ol>
<li>判斷是加法或減法:減法</li>
<li>若為減法則做二補數:$ (00010100)_2 $ 的二補數 $ (11101100)_2 $</li>
<li>相加<br>$$ \begin{split}<br>1&11111\ \ \ \ \ \ \ Carry(進位) \\<br>&11011101\ A \\</li>
</ol>
<p>+&\underline{00010100}\ (\overline{B}+1) \\<br>&11001001\ R(結果)<br>\end{split} $$</p>
<p>我們得到 (-35) - (+20) = (-55)</p>
<h2 id="整數-sign-and-magnitude-的加減法"><a href="#整數-sign-and-magnitude-的加減法" class="headerlink" title="整數(sign-and-magnitude)的加減法"></a>整數(sign-and-magnitude)的加減法</h2><p>整數(sign-and-magnitude)運算的過程:</p>
<p>$ A \pm B=? $</p>
<ol>
<li>判斷是加法或減法</li>
<li>若為減法則將 $ B_{Sign} $ 做NOT運算</li>
<li>指定S為 $ A_{Sign}\ XOR\ B_{Sign} $</li>
<li>若S等於1跳到第5步驟,否則跳到第6步驟</li>
<li><ol>
<li>$ R_{Magnitude} \leftarrow A_{Magnitude}+(\overline{B_{Magnitude}}+1) $</li>
<li>檢查是否overflow</li>
<li>如果overflow,$ R_{Sign} \leftarrow A_{Sign} $,然後結束,如果沒有跳到下一步驟</li>
<li>$ R_{Magnitude} \leftarrow (\overline{R_{Magnitude}}+1) $</li>
<li>$ R_{Sign} \leftarrow B_{Sign} $</li>
</ol>
</li>
<li><ol>
<li>$ R_{Magnitude} \leftarrow A_{Magnitude}+B_{Magnitude} $</li>
<li>檢查是否overflow</li>
<li>如果overflow,回到overflow然後結束,如果沒有跳到下一步驟</li>
<li>$ R_{Sign} \leftarrow A_{Sign} $</li>
</ol>
</li>
</ol>
<p>例1:A=$ (00010001)_2 $,B=$ (00010110)_2 $</p>
<p>A+B=?</p>
<ol>
<li>判斷是加法或減法:加法</li>
<li>若為減法則將 $ B_{Sign} $ 做NOT運算:跳過</li>
<li>指定S為 $ A_{Sign}\ XOR\ B_{Sign} $:S=0</li>
<li><ol>
<li>$ R_{Magnitude} \leftarrow A_{Magnitude}+B_{Magnitude} $:$ R_{Magnitude}=0100111 $</li>
<li>檢查是否overflow</li>
<li>如果overflow,回到overflow然後結束,如果沒有跳到下一步驟</li>
<li>$ R_{Sign} \leftarrow A_{Sign} $:$ R_{Sign}=0 $</li>
</ol>
</li>
</ol>
<p>這樣就得到結果 $ (+17)+(22)=(00100111)_2=(+39) $</p>
<p>例2:A=$ (00010001)_2 $,B=$ (10010110)_2 $</p>
<p>A+B=?</p>
<ol>
<li>判斷是加法或減法:加法</li>
<li>若為減法則將 $ B_{Sign} $ 做NOT運算:跳過</li>
<li>指定S為 $ A_{Sign}\ XOR\ B_{Sign} $:S=1</li>
<li>$ R_{Magnitude} \leftarrow A_{Magnitude}+(\overline{B_{Magnitude}}+1) $:$$ \begin{split}<br>No\ overflow\ \ \ \ \ \ &\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Carry(進位) \\<br>A_{Sign}\ 0\ \ \ \ \ \ &0010001\ A_{Magnitude} \\<br>B_{Sign}\ 1\ \ +&\underline{1101010}\ (\overline{B_{Magnitude}}+1) \\<br>&1111011\ R_{Magnitude} \\<br>R_{Sign}\ 1\ \ \ \ \ \ &0000101\ R_{Magnitude}= (\overline{R_{Magnitude}}+1) \\<br>\end{split} $$</li>
</ol>
<p>這樣就得到結果 $ (+17)+(-22)=(10000101)_2=(-5) $</p>
<p>例3:A=$ (11010001)_2 $,B=$ (10010110)_2 $</p>
<p>A-B=?</p>
<ol>
<li>判斷是加法或減法:減法</li>
<li>若為減法則將 $ B_{Sign} $ 做NOT運算</li>
<li>指定S為 $ A_{Sign}\ XOR\ B_{Sign} $:S=1</li>
<li><ol>
<li>$ R_{Magnitude} \leftarrow A_{Magnitude}+(\overline{B_{Magnitude}}+1) $:$ R_{Magnitude}=(0111011)_2 $</li>
<li>檢查是否overflow</li>
<li>如果overflow,$ R_{Sign} \leftarrow A_{Sign} $,然後結束:$ R_{Sign}=1 $</li>
</ol>
</li>
</ol>
<p>這樣就得到結果 $ (-81)+(-22)=(10111011)_2=(-59) $</p>
<h2 id="浮點數的加減法"><a href="#浮點數的加減法" class="headerlink" title="浮點數的加減法"></a>浮點數的加減法</h2><p>$ A \pm B=? $</p>
<p>浮點數運算的過程:</p>
<ol>
<li>如果其中一個數字為0,運算結果就為另一個數字</li>
<li>檢查是加法或減法</li>
<li>如果是減法,把 $ B_{Sign} $ 做NOT運算</li>
<li>將兩個數字去正規化(在尾數前面加個1 www)</li>
<li>將指數較小的數字累乘,並向右位移</li>
<li>將兩數相加(sign-and-magnitude)</li>
<li>檢查有沒有overflow</li>
<li>如果有overflow就將尾數右移,指數加一</li>
<li>正規化</li>
<li>如果需要捨去尾數則捨去</li>
</ol>
<p>例1:</p>
<p>A=$ (0\ 10000001\ 01110000000000000000000)_2 $<br>B=$ (0\ 10000110\ 01000011110000000000000)_2 $</p>
<p>A+B=?</p>
<ol>
<li>如果其中一個數字為0,運算結果就為另一個數字:跳過</li>
<li>檢查是加法或減法:加法</li>
<li>如果是減法,把 $ B_{Sign} $ 做NOT運算:跳過</li>
<li>將兩個數字去正規化(在尾數前面加個1 www):</li>
</ol>
<p>$ A_{Mantissa}=(101110000000000000000000)_2 $<br>$ B_{Mantissa}=(101000011110000000000000)_2 $</p>
<ol start="5">
<li>將指數較小的數字累乘,並向右位移</li>
</ol>
<p>A較小,指數加五,尾數右移五位</p>
<p>A_{Mantissa}=(000001011100000000000000) $</p>
<ol start="6">
<li>將兩數相加(sign-and-magnitude)</li>
</ol>
<p>R_{Mantissa}=(101001111010000000000000)_2</p>
<ol start="7">
<li>檢查有沒有overflow</li>
<li>如果有overflow就將尾數右移,指數加一:沒有</li>
<li>正規化</li>
</ol>
<p>$ R=(0\ 10000110\ 101001111010000000000000)_2 $</p>
<ol start="10">
<li>如果需要捨去尾數則捨去:跳過</li>
</ol>
<p>這樣就得到結果$ R=(010000110101001111010000000000000)_2 $</p>
<p>例2:</p>
<p>A=$ (0\ 10000001\ 01110000000000000000000)_2 $<br>B=$ (1\ 10000001\ 11000001100000000000000)_2 $</p>
<p>A-B=?</p>
<ol>
<li>如果其中一個數字為0,運算結果就為另一個數字:跳過</li>
<li>檢查是加法或減法:減法</li>
<li>如果是減法,把 $ B_{Sign} $ 做NOT運算</li>
<li>將兩個數字去正規化(在尾數前面加個1 www)</li>
</ol>
<p>$ A_{Mantissa}=(101110000000000000000000)_2 $<br>$ B_{Mantissa}=(111000001100000000000000)_2 $</p>
<ol start="5">
<li>將指數較小的數字累乘,並向右位移:兩數指數一樣,跳過</li>
<li>將兩數相加(sign-and-magnitude):</li>
</ol>
<p>$ R_{Sign}=(1)_2 $<br>$ R_{Mantissa}=(1010100011000000000000000)_2 $</p>
<ol start="7">
<li>檢查有沒有overflow:有</li>
<li>如果有overflow就將尾數右移,指數加一</li>
</ol>
<p>$ R_{Exponent}={10000010}_2 $<br>$ R_{Mantissa}=(0010100011000000000000000)_2 $</p>
<ol start="9">
<li>正規化</li>
</ol>
<p>$ R=(1\ 10000010\ 001010001100000000000000)_2 $</p>
<ol start="10">
<li>如果需要捨去尾數則捨去</li>
</ol>
<p>$ R=(1\ 10000010\ 00101000110000000000000)_2 $</p>
]]></content>
<categories>
<category>計算機概論</category>
</categories>
<tags>
<tag>計算機概論</tag>
</tags>
</entry>
<entry>
<title>C語言-Hello World</title>
<url>/2015/10/17/C%E8%AA%9E%E8%A8%80-Hello-World/</url>
<content><![CDATA[<h1 id="C語言歷史"><a href="#C語言歷史" class="headerlink" title="C語言歷史"></a>C語言歷史</h1><p>以下資料均出自維基百科</p>
<ul>
<li><p>1969 ~ 1973年:C語言最早由丹尼斯·里奇(Dennis Ritchie)為了在PDP-11電腦上運行的Unix系統所設計出來的程式語言。C源於BCPL語言,後者由馬丁·理察德(Martin Richards)於1967年左右設計實作。BCPL是一門”沒有資料類性”的程式語言:它僅能操作一種資料類型,即機器字(machine word)。</p>
</li>
<li><p>1970年,肯·湯普遜為執行在PDP-7上的首個Unix系統設計了一個精簡版的BCPL,這個語言被稱為B語言,它也是沒有資料類性的。Unix最早運行在PDP-7上,是以組合語言寫成。在PDP-11出現後,丹尼斯·里奇與肯·湯普遜著手將Unix移植到PDP-11上,無類型的語言在PDP-11上愈發顯得合適。PDP-11提供了多種不同規格大小的基本物件:一位元組長的字符,兩位元組長的整型數以及四位元組長的浮點數。B語言無法處理這些不同規格大小的物件,也沒有提供單獨的操作符去操作它們。C語言最初嘗試通過向B語言中增加資料類型的想法來處理那些不同類型的資料。和大多數語言一樣,在C中,每個物件都有一個類型以及一個值;類型決定了可用於值的操作的含義,以及物件占用的儲存空間大小。</p>
</li>
<li><p>1973年,Unix作業系統的核心正式用C語言改寫,這是C語言第一次應用在作業系統的核心編寫上。</p>
</li>
<li><p>1975年C語言開始移植到其他機器上使用。史蒂芬·強生實作了一套「可移植編譯器」,這套編譯器修改起來相對容易,並且可以為不同的機器生成代碼。從那時起,C在大多數電腦上被使用,從最小的微型電腦到與CRAY-2超級電腦。C語言很規範,即使沒有一份正式的標準,你也可以寫出C程式,這些程式無須修改就可以執行在任何支援C語言和最小執行時環境的電腦上。C最初在小型機器上實作,並且繼承了一系列小語種程式語言的特點;與功能相比,C的設計者更傾向於簡單和優雅。此外,從一開始,C語言就是為系統級編程而設計,程式的執行效率至關重要,因此,C語言與真實機器能力的良好匹配也就不足為奇。例如,C語言為典型硬體所直接支援的物件:字符,整數(也許有多種大小),以及浮點數(同樣可能有多種大小)提供了相應的基本資料類型。</p>
</li>
<li><p>1978年,丹尼斯·里奇和布萊恩·柯林漢合作出版了《C程式設計語言》的第一版。書中介紹的C語言標準也被C語言程式設計師稱作「K&R C」,第二版的書中也包含了一些ANSI C的標準。即使在後來ANSI C標準被提出的許多年後,K&R C仍然是許多編譯器的最低標準要求,許多老舊的編譯仍然運行K&R C的標準。</p>
</li>
<li><p>1989年,C語言被美國國家標準協會(ANSI)標準化,編號為ANSI X3.159-1989。這個版本又稱為C89。標準化的一個目的是擴充功能K&R C,增加了一些新特性。</p>
</li>
<li><p>1990年,國際標準化組織(ISO)成立 ISO/IEC JTC1/SC22/WG14 工作群組,來規定國際標準的C語言,通過對ANSI標準的少量修改,最終製定了 ISO 9899:1990,又稱為C90。隨後,ANSI亦接受國際標準C,並不再發展新的C標準。</p>
</li>
<li><p>2000年,採用1999年ISO 9899:1999的發表,稱為C99</p>
</li>
<li><p>2011年,ISO正式發行了新的C語言的新標準C11,之前被稱為C1X,官方名稱為ISO/IEC 9899:2011。</p>
</li>
</ul>
<h1 id="Hello-World"><a href="#Hello-World" class="headerlink" title="Hello World"></a>Hello World</h1><p>學一個語言就是要先學它的髒話,所以我們要講說C語言的髒話。</p>
<p>讓我們來看<del>Hell World</del>Hello World範例程式。</p>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="comment">//這是單行註解</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">這是</span></span><br><span class="line"><span class="comment"> 多行</span></span><br><span class="line"><span class="comment"> 註解</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stdlib.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">void</span>)</span></span>{</span><br><span class="line"></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"Hello, world!\n"</span>);</span><br><span class="line"> system(<span class="string">"pause"</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>讓我們來好好解釋這些程式碼</p>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="comment">//這是單行註解</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">這是</span></span><br><span class="line"><span class="comment"> 多行</span></span><br><span class="line"><span class="comment"> 註解</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<p>註解是不會被執行的,註解主要目的為解釋程式碼,以便後人接手你的程式碼的時候不會看不懂,以提高程式碼的可維護性。</p>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><stdio.h></span></span></span><br></pre></td></tr></table></figure>
<p>#include是巨集前置處理器(Macro Preprocessor),會告訴編譯器這支程式會使用到 stdio.h 標頭檔,標頭檔通常定義了一些常用的東西讓我們使用,例如 printf 就是在螢幕上顯示文字。</p>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>{</span><br><span class="line"> ...</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>main為程式的主體,也是程式的進入點,為程式開始的地方。</p>
<p>return 是程式的回傳值,通常為 0。</p>
<p>為什麼回傳值為零請參考此<a href="https://www.ptt.cc/bbs/C_and_CPP/M.1316185874.A.C43.html">連結</a>。</p>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="built_in">printf</span>(<span class="string">"Hello, world!\n"</span>);</span><br></pre></td></tr></table></figure>
<p>所有陳述(Statement)結尾都要加 ;</p>
<p>printf 是將雙引號內的文字顯示到螢幕上</p>
<p>\n是跳脫字元,表示換行</p>
<h1 id="參考"><a href="#參考" class="headerlink" title="參考"></a>參考</h1><ol>
<li><a href="https://zh.wikipedia.org/wiki/C%E8%AF%AD%E8%A8%80">C語言</a></li>
<li><a href="http://openhome.cc/Gossip/CGossip/HelloWorld.html">Hello! World!</a></li>
</ol>
]]></content>
<categories>
<category>C語言</category>
</categories>
<tags>
<tag>C語言</tag>
</tags>
</entry>
<entry>
<title>C語言-資料型態與變數運算</title>
<url>/2015/10/19/C%E8%AA%9E%E8%A8%80-%E8%B3%87%E6%96%99%E5%9E%8B%E6%85%8B%E8%88%87%E8%AE%8A%E6%95%B8%E9%81%8B%E7%AE%97/</url>
<content><![CDATA[<h1 id="資料型態"><a href="#資料型態" class="headerlink" title="資料型態"></a>資料型態</h1><p>程式在執行的過程中需要許多的資訊,而這些資訊都儲存在記憶體空間中</p>
<p>而根據資訊的用途與所需空間的不同,所以訂定了<strong>資料型態</strong>的規範</p>
<p>資料型態分為</p>
<ul>
<li><p>整數:用來表示整數,其所佔的記憶體空間有可能因為編譯器而有所不同,可以使用unsigned修飾字讓有號整數變成無號整數</p>
<ul>
<li>short (短整數,佔2位元組)</li>
<li>int (整數,佔4位元組)</li>
<li>unsigned (無號整數,佔4位元組)</li>
<li>long (長整數,佔4位元組)</li>
<li>long long int (長長整數,佔8位元組)</li>
</ul>
</li>
<li><p>浮點數:可以用來表示小數</p>
<ul>
<li>float (單精度浮點數,佔4位元組,精度只到小數點以下7位)</li>
<li>double (雙精度浮點數,佔8位元組,精度只到小數點以下15位)</li>
<li>long double (長雙精度浮點數,佔16位元組)</li>
</ul>
</li>
<li><p>字元:字元編碼依ASCII表編成,因為所佔有空間小,也可以表達小範圍的整數</p>
<ul>
<li>char (字元,佔1位元組)</li>
</ul>
</li>
</ul>
<p>以下為示範程式,用TDM-GCC 4.9.2編譯的</p>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stdlib.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">void</span>)</span> </span>{</span><br><span class="line"> </span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"型態\t\t大小(bytes)\n"</span>); </span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"short\t\t%d\n"</span>, <span class="keyword">sizeof</span>(<span class="keyword">short</span>)); </span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"int\t\t%d\n"</span>, <span class="keyword">sizeof</span>(<span class="keyword">int</span>));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"unsigned\t\t%d\n"</span>, <span class="keyword">sizeof</span>(<span class="keyword">int</span>));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"long\t\t%d\n"</span>, <span class="keyword">sizeof</span>(<span class="keyword">long</span>));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"long long\t%d\n"</span>, <span class="keyword">sizeof</span>(<span class="keyword">long</span> <span class="keyword">long</span>));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"float\t\t%d\n"</span>, <span class="keyword">sizeof</span>(<span class="keyword">float</span>));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"double\t\t%d\n"</span>, <span class="keyword">sizeof</span>(<span class="keyword">double</span>)); </span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"long double\t%d\n"</span>, <span class="keyword">sizeof</span>(<span class="keyword">long</span> <span class="keyword">double</span>)); </span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"char\t\t%d\n"</span>, <span class="keyword">sizeof</span>(<span class="keyword">char</span>)); </span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> </span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>\t為跳脫字元,用來對齊下一個顯示位置</p>
<p>%d為格式指定碼,表示這個位置要放入整數,然後被後面sizeof的運算結果取代</p>
<p>sizeof的功能是取得該型態所佔的記憶體大小</p>
<p>以下為執行結果</p>
<pre><code>型態 大小(bytes)
short 2
int 4
unsigned 4
long 4
long long 8
float 4
double 8
long double 16
char 1</code></pre>
<p>以下為型別的範圍大小</p>
<table>
<thead>
<tr>
<th>型別</th>
<th>最大值</th>
<th>最小值</th>
</tr>
</thead>
<tbody><tr>
<td>char</td>
<td>127</td>
<td>-128</td>
</tr>
<tr>
<td>short</td>
<td>32767</td>
<td>-32768</td>
</tr>
<tr>
<td>int</td>
<td>2147483647</td>
<td>-2147483648</td>
</tr>
<tr>
<td>unsigned</td>
<td>4294967296</td>
<td>0</td>
</tr>
<tr>
<td>long</td>
<td>2147483647</td>
<td>-2147483648</td>
</tr>
<tr>
<td>long long</td>
<td>9223372036854775807</td>
<td>-9223372036854775808</td>
</tr>
<tr>
<td>float</td>
<td>$3.40282*10^{38}$</td>
<td>$1.17549*10^{-38}$</td>
</tr>
<tr>
<td>double</td>
<td>$1.79769*10^{308}$</td>
<td>$2.22507*10^{-308}$</td>
</tr>
<tr>
<td>long double</td>
<td>$1.18973*10^{4932}$</td>
<td>$3.3621*10^{-4932}$</td>
</tr>
</tbody></table>
<h1 id="變數"><a href="#變數" class="headerlink" title="變數"></a>變數</h1><p>有了資料型態之後要怎麼把資料存在記憶體呢?答案是變數</p>
<p>變數簡單說就是一個代表某種資料型態且有名稱的記憶體空間</p>
<p>要使用變數之前,必須先宣告變數名稱跟資料型態,以下是變數宣告的範例</p>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="keyword">int</span> num; <span class="comment">// 宣告一個整數變數</span></span><br></pre></td></tr></table></figure>
<p>‘=’是指定符號,也就是將某個變數代入某個值</p>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line">score = <span class="number">100</span>;</span><br><span class="line">level = <span class="string">'A'</span>;</span><br></pre></td></tr></table></figure>
<p>有些名稱是不能拿來當變數名稱的,稱為<strong>保留字</strong>,以下為保留字</p>
<table>
<thead>
<tr>
<th>保留字</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody><tr>
<td>char</td>
<td>short</td>
<td>int</td>
<td>unsigned</td>
</tr>
<tr>
<td>long</td>
<td>float</td>
<td>double</td>
<td>struct</td>
</tr>
<tr>
<td>union</td>
<td>void</td>
<td>enum</td>
<td>signed</td>
</tr>
<tr>
<td>const</td>
<td>volatile</td>
<td>typedef</td>
<td>auto</td>
</tr>
<tr>
<td>register</td>
<td>static</td>
<td>extern</td>
<td>break</td>
</tr>
<tr>
<td>case</td>
<td>continue</td>
<td>default</td>
<td>do</td>
</tr>
<tr>
<td>else</td>