-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchap1_2binaryEncoding.html
531 lines (418 loc) · 18 KB
/
chap1_2binaryEncoding.html
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
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2.2. Binary encoding — Computing for engineer 1.0 documentation</title>
<script type="text/javascript" src="_static/js/modernizr.min.js"></script>
<script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="_static/language_data.js"></script>
<script type="text/javascript" src="_static/js/theme.js"></script>
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="2.3. Digital circuits" href="chap1_3digitalCircuits.html" />
<link rel="prev" title="2.1. Propositional Logic" href="chap1_1propositions.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" >
<a href="index.html" class="icon icon-home"> Computing for engineer
</a>
<div class="version">
1.0
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<p class="caption"><span class="caption-text">Contents:</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="Introduction.html">1. Introduction</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="chap1_binaryLogic_Chap.html">2. Binary logic</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="chap1_1propositions.html">2.1. Propositional Logic</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">2.2. Binary encoding</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#encoding-using-numbers">2.2.1. Encoding using numbers</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#encoding-text">2.2.1.1. Encoding text</a></li>
<li class="toctree-l4"><a class="reference internal" href="#encoding-images">2.2.1.2. Encoding images</a></li>
<li class="toctree-l4"><a class="reference internal" href="#other-encoding-examples">2.2.1.3. Other encoding examples</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="#binary-algebra">2.2.2. Binary algebra</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#encoding-positive-integers">2.2.2.1. Encoding positive integers</a></li>
<li class="toctree-l4"><a class="reference internal" href="#encoding-negative-integers">2.2.2.2. Encoding negative integers</a></li>
<li class="toctree-l4"><a class="reference internal" href="#encoding-real-numbers">2.2.2.3. Encoding real numbers</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="chap1_3digitalCircuits.html">2.3. Digital circuits</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="chap2_architecture_Chap.html">3. Computer architecture</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap3_algorithm_Chap.html">4. Algorithm</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap4_cLangage_Chap.html">5. C/C++ Programming Langage</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap5_arduino_Chap.html">6. Arduino</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap6_python_Chap.html">7. Python</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap7_C++_Chap.html">8. C++</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="index.html">Computing for engineer</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="index.html">Docs</a> »</li>
<li><a href="chap1_binaryLogic_Chap.html">2. Binary logic</a> »</li>
<li>2.2. Binary encoding</li>
<li class="wy-breadcrumbs-aside">
<a href="_sources/chap1_2binaryEncoding.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="binary-encoding">
<h1>2.2. Binary encoding<a class="headerlink" href="#binary-encoding" title="Permalink to this headline">¶</a></h1>
<p>Informatics systems are treating a lot of information (sensors, text, images, musics, videos, games, etc.) called data. Whatever the form of the data is, everything on a computer is encoded as numbers, such that a computer is finally a big calculator with specific input and output peripherals (keyboard, screen, etc.).</p>
<div class="section" id="encoding-using-numbers">
<h2>2.2.1. Encoding using numbers<a class="headerlink" href="#encoding-using-numbers" title="Permalink to this headline">¶</a></h2>
<p>Whatever the origin of the data is, the computer is treating it as numbers. Here are presented some examples of existing encoding.</p>
<div class="section" id="encoding-text">
<h3>2.2.1.1. Encoding text<a class="headerlink" href="#encoding-text" title="Permalink to this headline">¶</a></h3>
<p>The simpler way to encode a text is to associate an integer number to each possible character. For example, in the English language, there are 26 letters and some special characters (%<,;:! etc.). The ASCII code is encoding characters with a value range between 0 and 127.</p>
<div class="figure align-center" id="id1">
<span id="fig-chap1-ascii"></span><a class="reference internal image-reference" href="_images/ASCII.jpg"><img alt="_images/ASCII.jpg" src="_images/ASCII.jpg" style="width: 500.0px; height: 406.0px;" /></a>
<p class="caption"><span class="caption-number">Figure 2.2: </span><span class="caption-text">ASCII table for character encoding</span></p>
</div>
<p>As example, we see that the ‘a’ character is represented by the number 97 in the ASCII table.</p>
</div>
<div class="section" id="encoding-images">
<h3>2.2.1.2. Encoding images<a class="headerlink" href="#encoding-images" title="Permalink to this headline">¶</a></h3>
<p>For images, this is similar to text. An image is discretized in pixels. Typically, a full HD image is composed with 1920 x 1080 pixels, that is to say more than 2 million pixels. A pixel is a small square with a unique value for each primary colors (Red, Green and Blue) ranged between 0 and 255.</p>
<div class="figure align-center" id="id2">
<span id="fig-chap1-rgb"></span><a class="reference internal image-reference" href="_images/RGB.jpg"><img alt="_images/RGB.jpg" src="_images/RGB.jpg" style="width: 373.8px; height: 181.3px;" /></a>
<p class="caption"><span class="caption-number">Figure 2.3: </span><span class="caption-text">RGB encoding example (<a class="reference external" href="https://htmlcolorcodes.com/fr/">https://htmlcolorcodes.com/fr/</a>)</span></p>
</div>
</div>
<div class="section" id="other-encoding-examples">
<h3>2.2.1.3. Other encoding examples<a class="headerlink" href="#other-encoding-examples" title="Permalink to this headline">¶</a></h3>
<p>Other information are also encoded:</p>
<ul class="simple">
<li>A sound needs pitch and intensity encoding.</li>
<li>A video is composed with a lot of images.</li>
</ul>
</div>
</div>
<div class="section" id="binary-algebra">
<h2>2.2.2. Binary algebra<a class="headerlink" href="#binary-algebra" title="Permalink to this headline">¶</a></h2>
<p>Thus, each data of informatics systems are encoded as numbers. But how these numbers are stored themselves physically in an electronic system ? This is ensured thanks to electronic components that may let a current pass (1) or not (0) acting like switches.
For example, a unique switch is able to encode 2 different information:</p>
<ul class="simple">
<li>the switch is open: the current do not pass (0)</li>
<li>the switch is closed: the current pass (1)</li>
</ul>
<p>If we use a second switch, 4 different information may be encoded:</p>
<ul class="simple">
<li>switch A is open:<ul>
<li>switch B is open: (00)</li>
<li>switch B is closed: (01)</li>
</ul>
</li>
<li>switch A is closed:<ul>
<li>switch B is open: (10)</li>
<li>switch B is closed: (11)</li>
</ul>
</li>
</ul>
<p>With n switches, <img class="math" src="_images/math/d337c3294387cc79893f21514a2b0a51e3894ed2.svg" alt="2^n"/> different information may be encoded.</p>
<p>In informatics the elementary data that can be stored electronically is named a <strong>bit</strong> and can be set to <em>0</em> or <em>1</em>.</p>
<p>8 bits are grouped to form a <strong>byte</strong>.</p>
<div class="section" id="encoding-positive-integers">
<h3>2.2.2.1. Encoding positive integers<a class="headerlink" href="#encoding-positive-integers" title="Permalink to this headline">¶</a></h3>
<p>Binary encoding of positive integers is the more natural. Let us consider that the information to store is encoded on 8 bits (or 1 byte), the range of integer will be from 0 to <img class="math" src="_images/math/9ca2e8cb4a5ab2432970dc7b92fcccf1090a4d58.svg" alt="2^8 - 1 = 255"/>.</p>
<table border="1" class="docutils">
<colgroup>
<col width="35%" />
<col width="65%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">integer</th>
<th class="head">binary encoding</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>0</td>
<td>00000000</td>
</tr>
<tr class="row-odd"><td>1</td>
<td>00000001</td>
</tr>
<tr class="row-even"><td>2</td>
<td>00000010</td>
</tr>
<tr class="row-odd"><td>3</td>
<td>00000011</td>
</tr>
<tr class="row-even"><td>4</td>
<td>00000100</td>
</tr>
<tr class="row-odd"><td>5</td>
<td>00000101</td>
</tr>
<tr class="row-even"><td>…</td>
<td>…</td>
</tr>
<tr class="row-odd"><td>255</td>
<td>11111111</td>
</tr>
</tbody>
</table>
<p>To generalize, every integer may be written as a sum of power of 2.</p>
<div class="math">
<p><img src="_images/math/356a3ace9c537658a003e463348fcac1b349a6d3.svg" alt="\forall i \in \mathbb{N}, i = b_0 2^0 + b_1 2^1 + b_2 2^2 + ... + b_n 2^n"/></p>
</div><p>The binary representation of an integer is thus:</p>
<table border="1" class="docutils">
<colgroup>
<col width="20%" />
<col width="20%" />
<col width="20%" />
<col width="20%" />
<col width="20%" />
</colgroup>
<tbody valign="top">
<tr class="row-odd"><td>bn</td>
<td>…</td>
<td>b2</td>
<td>b1</td>
<td>b0</td>
</tr>
</tbody>
</table>
<p>Integers are generally encoded on 4 bytes (32 bits).</p>
<div class="section" id="convert-an-integer-in-binary">
<h4>2.2.2.1.1. Convert an integer in binary<a class="headerlink" href="#convert-an-integer-in-binary" title="Permalink to this headline">¶</a></h4>
<p>To convert easily an integer in binary, a simple way is to apply successive entire division by 2. As example, let us consider the number 156:</p>
<table border="1" class="docutils">
<colgroup>
<col width="34%" />
<col width="31%" />
<col width="34%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">operation</th>
<th class="head">quotient</th>
<th class="head">remainder</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>156/2</td>
<td>78</td>
<td><strong>0</strong></td>
</tr>
<tr class="row-odd"><td>78/2</td>
<td>39</td>
<td><strong>0</strong></td>
</tr>
<tr class="row-even"><td>39/2</td>
<td>19</td>
<td><strong>1</strong></td>
</tr>
<tr class="row-odd"><td>19/2</td>
<td>9</td>
<td><strong>1</strong></td>
</tr>
<tr class="row-even"><td>9/2</td>
<td>4</td>
<td><strong>1</strong></td>
</tr>
<tr class="row-odd"><td>4/2</td>
<td>2</td>
<td><strong>0</strong></td>
</tr>
<tr class="row-even"><td>2/2</td>
<td>1</td>
<td><strong>0</strong></td>
</tr>
<tr class="row-odd"><td>1/2</td>
<td>0</td>
<td><strong>1</strong></td>
</tr>
</tbody>
</table>
<p><img class="math" src="_images/math/ab9543fdece52bee74af22a32c3d6bd4c09ae8be.svg" alt="156 = 0 \times 2^0 + 0 \times 2^1 + 1 \times 2^2 + 1 \times 2^3 + 1 \times 2^4 + 0 \times 2^5 + 0 \times 2^6 + 1 \times 2^7"/></p>
<blockquote>
<div><table border="1" class="docutils">
<colgroup>
<col width="13%" />
<col width="13%" />
<col width="13%" />
<col width="13%" />
<col width="13%" />
<col width="13%" />
<col width="13%" />
<col width="13%" />
</colgroup>
<tbody valign="top">
<tr class="row-odd"><td>1</td>
<td>0</td>
<td>0</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>0</td>
<td>0</td>
</tr>
</tbody>
</table>
</div></blockquote>
</div>
</div>
<div class="section" id="encoding-negative-integers">
<h3>2.2.2.2. Encoding negative integers<a class="headerlink" href="#encoding-negative-integers" title="Permalink to this headline">¶</a></h3>
<p>Several methods exist to encode a negative number, but the more efficient is those using the two’s complement and act in three steps:</p>
<ol class="arabic simple">
<li>The absolute value of the number is encoded in binary.</li>
<li>All bits are reversed (0 becomes 1 and reversely).</li>
<li>1 is added to the result.</li>
</ol>
<p>As example, the negative number -42 will be encoding by these three steps:</p>
<table border="1" class="docutils">
<colgroup>
<col width="22%" />
<col width="11%" />
<col width="11%" />
<col width="11%" />
<col width="11%" />
<col width="11%" />
<col width="11%" />
<col width="11%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"> </th>
<th class="head">b6</th>
<th class="head">b5</th>
<th class="head">b4</th>
<th class="head">b3</th>
<th class="head">b2</th>
<th class="head">b1</th>
<th class="head">b0</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>step 1</td>
<td>0</td>
<td>1</td>
<td>0</td>
<td>1</td>
<td>0</td>
<td>1</td>
<td>0</td>
</tr>
<tr class="row-odd"><td>step 2</td>
<td>1</td>
<td>0</td>
<td>1</td>
<td>0</td>
<td>1</td>
<td>0</td>
<td>1</td>
</tr>
<tr class="row-even"><td>step 3</td>
<td>1</td>
<td>0</td>
<td>1</td>
<td>0</td>
<td>1</td>
<td>1</td>
<td>0</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="encoding-real-numbers">
<h3>2.2.2.3. Encoding real numbers<a class="headerlink" href="#encoding-real-numbers" title="Permalink to this headline">¶</a></h3>
<p>Real number are encoded thanks to the IEEE754 standard using 32 bits (4 bytes). The objective is to express the real number using a scientific representation in binary. The steps are:</p>
<ol class="arabic simple">
<li>The real number is decomposed in power of 2 the entire and decimal part of the real number.</li>
<li>The binary encoding is then normalized in scientific form.</li>
<li>The first bit is used for the sign (0 for positive real number). The mantissa is using 23 bits and the exponent is augmented by 127 and encoded on 8 bits.</li>
</ol>
<p>Let us take an example: the number 197,75.</p>
<div class="math">
<p><img src="_images/math/6ce9b0795ab4f4f7f06a621b5efba9e77e23449a.svg" alt="\[
\begin{array}{ccc}
197,75 & = & 128 + 64 + 4 + 1 , 0.5 + 0.25 \\
& = & 2^7 + 2^6 + 2^2 + 2^0, 2^{-1} + 2^{-2} \\
& = & 11000101,11 \\
& = & 1,100010111 \times 2^7
\end{array}
\]"/></p>
</div><p>The binary representation of 197,75 is then:</p>
<table border="1" class="docutils">
<colgroup>
<col width="20%" />
<col width="38%" />
<col width="42%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">sign (b31)</th>
<th class="head">exponent (b23 to b30)</th>
<th class="head">mantissa (b0 to b22)</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>0</td>
<td>10000110</td>
<td>10001011100000000000000</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="chap1_3digitalCircuits.html" class="btn btn-neutral float-right" title="2.3. Digital circuits" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="chap1_1propositions.html" class="btn btn-neutral float-left" title="2.1. Propositional Logic" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
© Copyright 2019, Polytech ME Info team : LB, CM, FP, JV
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>