Code

Imported upstream version 1.2.26
[pkg-rrdtool.git] / doc / bin_dec_hex.html
1 <?xml version="1.0" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <title>bin_dec_hex</title>
6 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
7 <link rev="made" href="mailto:root@localhost" />
8 </head>
10 <body style="background-color: white">
12 <p><a name="__index__"></a></p>
13 <!-- INDEX BEGIN -->
14 <!--
16 <ul>
18         <li><a href="#name">NAME</a></li>
19         <li><a href="#description">DESCRIPTION</a></li>
20         <li><a href="#author">AUTHOR</a></li>
21 </ul>
22 -->
23 <!-- INDEX END -->
25 <p>
26 </p>
27 <h1><a name="name">NAME</a></h1>
28 <p>bin_dec_hex - How to use binary, decimal, and hexadecimal notation.</p>
29 <div align="right"><a href="bin_dec_hex.pdf">PDF</a> version.</div><p>
30 </p>
31 <hr />
32 <h1><a name="description">DESCRIPTION</a></h1>
33 <p>Most people use the decimal numbering system. This system uses ten
34 symbols to represent numbers. When those ten symbols are used up, they
35 start all over again and increment the position to the left. The
36 digit 0 is only shown if it is the only symbol in the sequence, or if
37 it is not the first one.</p>
38 <p>If this sounds cryptic to you, this is what I've just said in numbers:</p>
39 <pre>
40      0
41      1
42      2
43      3
44      4
45      5
46      6
47      7
48      8
49      9
50     10
51     11
52     12
53     13</pre>
54 <p>and so on.</p>
55 <p>Each time the digit nine is incremented, it is reset to 0 and the
56 position before (to the left) is incremented (from 0 to 1). Then
57 number 9 can be seen as ``00009'' and when we should increment 9, we
58 reset it to zero and increment the digit just before the 9 so the
59 number becomes ``00010''. Leading zeros we don't write except if it is
60 the only digit (number 0). And of course, we write zeros if they occur
61 anywhere inside or at the end of a number:</p>
62 <pre>
63  &quot;00010&quot; -&gt; &quot; 0010&quot; -&gt; &quot; 010&quot; -&gt; &quot;  10&quot;, but not &quot;  1 &quot;.</pre>
64 <p>This was pretty basic, you already knew this. Why did I tell it?
65 Well, computers usually do not represent numbers with 10 different
66 digits. They only use two different symbols, namely ``0'' and ``1''. Apply
67 the same rules to this set of digits and you get the binary numbering
68 system:</p>
69 <pre>
70      0
71      1
72     10
73     11
74    100
75    101
76    110
77    111
78   1000
79   1001
80   1010
81   1011
82   1100
83   1101</pre>
84 <p>and so on.</p>
85 <p>If you count the number of rows, you'll see that these are again 14
86 different numbers. The numbers are the same and mean the same as in
87 the first list, we just used a different representation. This means
88 that you have to know the representation used, or as it is called the
89 numbering system or base.  Normally, if we do not explicitly specify
90 the numbering system used, we implicitly use the decimal system. If we
91 want to use any other numbering system, we'll have to make that
92 clear. There are a few widely adopted methods to do so. One common
93 form is to write <code>1010(2)</code> which means that you wrote down a number in
94 its binary representation. It is the number ten. If you would write
95 1010 without specifying the base, the number is interpreted as one
96 thousand and ten using base 10.</p>
97 <p>In books, another form is common. It uses subscripts (little
98 characters, more or less in between two rows). You can leave out the
99 parentheses in that case and write down the number in normal
100 characters followed by a little two just behind it.</p>
101 <p>As the numbering system used is also called the base, we talk of the
102 number 1100 base 2, the number 12 base 10.</p>
103 <p>Within the binary system, it is common to write leading zeros. The
104 numbers are written down in series of four, eight or sixteen depending
105 on the context.</p>
106 <p>We can use the binary form when talking to computers
107 (...programming...), but the numbers will have large
108 representations. The number 65'535 (often in the decimal system a ' is
109 used to separate blocks of three digits for readability) would be
110 written down as <code>1111111111111111(2)</code> which is 16 times the digit 1.
111 This is difficult and prone to errors. Therefore, we usually would use
112 another base, called hexadecimal. It uses 16 different symbols. First
113 the symbols from the decimal system are used, thereafter we continue
114 with alphabetic characters. We get 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
115 A, B, C, D, E and F. This system is chosen because the hexadecimal
116 form can be converted into the binary system very easily (and back).</p>
117 <p>There is yet another system in use, called the octal system. This was
118 more common in the old days, but is not used very often anymore. As
119 you might find it in use sometimes, you should get used to it and
120 we'll show it below. It's the same story as with the other
121 representations, but with eight different symbols.</p>
122 <pre>
123  Binary      (2)
124  Octal       (8)
125  Decimal     (10)
126  Hexadecimal (16)</pre>
127 <pre>
128  (2)    (8) (10) (16)
129  00000   0    0    0
130  00001   1    1    1
131  00010   2    2    2
132  00011   3    3    3
133  00100   4    4    4
134  00101   5    5    5
135  00110   6    6    6
136  00111   7    7    7
137  01000  10    8    8
138  01001  11    9    9
139  01010  12   10    A
140  01011  13   11    B
141  01100  14   12    C
142  01101  15   13    D
143  01110  16   14    E
144  01111  17   15    F
145  10000  20   16   10
146  10001  21   17   11
147  10010  22   18   12
148  10011  23   19   13
149  10100  24   20   14
150  10101  25   21   15</pre>
151 <p>Most computers used nowadays are using bytes of eight bits. This means
152 that they store eight bits at a time. You can see why the octal system
153 is not the most practical for that: You'd need three digits to represent
154 the eight bits and this means that you'd have to use one complete digit
155 to represent only two bits (2+3+3=8). This is a waste. For hexadecimal
156 digits, you need only two digits which are used completely:</p>
157 <pre>
158  (2)      (8)  (10) (16)
159  11111111 377  255   FF</pre>
160 <p>You can see why binary and hexadecimal can be converted quickly: For
161 each hexadecimal digit there are exactly four binary digits.  Take a
162 binary number: take four digits from the right and make a hexadecimal
163 digit from it (see the table above). Repeat this until there are no
164 more digits. And the other way around: Take a hexadecimal number. For
165 each digit, write down its binary equivalent.</p>
166 <p>Computers (or rather the parsers running on them) would have a hard
167 time converting a number like 1234(16). Therefore hexadecimal numbers
168 are specified with a prefix. This prefix depends on the language
169 you're writing in. Some of the prefixes are ``0x'' for C, ``$'' for
170 Pascal, ``#'' for HTML.  It is common to assume that if a number starts
171 with a zero, it is octal. It does not matter what is used as long as
172 you know what it is. I will use ``0x'' for hexadecimal, ``%'' for binary
173 and ``0'' for octal.  The following numbers are all the same, just their represenatation (base) is different: 021 0x11 17 %00010001</p>
174 <p>To do arithmetics and conversions you need to understand one more thing.
175 It is something you already know but perhaps you do not ``see'' it yet:</p>
176 <p>If you write down 1234, (no prefix, so it is decimal) you are talking
177 about the number one thousand, two hundred and thirty four. In sort of
178 a formula:</p>
179 <pre>
180  1 * 1000 = 1000
181  2 *  100 =  200
182  3 *   10 =   30
183  4 *    1 =    4</pre>
184 <p>This can also be written as:</p>
185 <pre>
186  1 * 10^3
187  2 * 10^2
188  3 * 10^1
189  4 * 10^0</pre>
190 <p>where ^ means ``to the power of''.</p>
191 <p>We are using the base 10, and the positions 0,1,2 and 3.
192 The right-most position should NOT be multiplied with 10. The second
193 from the right should be multiplied one time with 10. The third from
194 the right is multiplied with 10 two times. This continues for whatever
195 positions are used.</p>
196 <p>It is the same in all other representations:</p>
197 <p>0x1234 will be</p>
198 <pre>
199  1 * 16^3
200  2 * 16^2
201  3 * 16^1
202  4 * 16^0</pre>
203 <p>01234 would be</p>
204 <pre>
205  1 * 8^3
206  2 * 8^2
207  3 * 8^1
208  4 * 8^0</pre>
209 <p>This example can not be done for binary as that system only uses two
210 symbols. Another example:</p>
211 <p>%1010 would be</p>
212 <pre>
213  1 * 2^3
214  0 * 2^2
215  1 * 2^1
216  0 * 2^0</pre>
217 <p>It would have been easier to convert it to its hexadecimal form and
218 just translate %1010 into 0xA. After a while you get used to it. You will
219 not need to do any calculations anymore, but just know that 0xA means 10.</p>
220 <p>To convert a decimal number into a hexadecimal you could use the next
221 method. It will take some time to be able to do the estimates, but it
222 will be easier when you use the system more frequently. We'll look at
223 yet another way afterwards.</p>
224 <p>First you need to know how many positions will be used in the other
225 system. To do so, you need to know the maximum numbers you'll be
226 using. Well, that's not as hard as it looks. In decimal, the maximum
227 number that you can form with two digits is ``99''. The maximum for
228 three: ``999''. The next number would need an extra position. Reverse
229 this idea and you will see that the number can be found by taking 10^3
230 (10*10*10 is 1000) minus 1 or 10^2 minus one.</p>
231 <p>This can be done for hexadecimal as well:</p>
232 <pre>
233  16^4 = 0x10000 = 65536
234  16^3 =  0x1000 =  4096
235  16^2 =   0x100 =   256
236  16^1 =    0x10 =    16</pre>
237 <p>If a number is smaller than 65'536 it will fit in four positions.
238 If the number is bigger than 4'095, you must use position 4.
239 How many times you can subtract 4'096 from the number without going below
240 zero is the first digit you write down. This will always be a number
241 from 1 to 15 (0x1 to 0xF). Do the same for the other positions.</p>
242 <p>Let's try with 41'029. It is smaller than 16^4 but bigger than 16^3-1. This
243 means that we have to use four positions.
244 We can subtract 16^3 from 41'029 ten times without going below zero.
245 The left-most digit will therefore be ``A'', so we have 0xA????.
246 The number is reduced to 41'029 - 10*4'096 = 41'029-40'960 = 69.
247 69 is smaller than 16^3 but not bigger than 16^2-1. The second digit
248 is therefore ``0'' and we now have 0xA0??.
249 69 is smaller than 16^2 and bigger than 16^1-1. We can subtract 16^1
250 (which is just plain 16) four times and write down ``4'' to get 0xA04?.
251 Subtract 64 from 69 (69 - 4*16) and the last digit is 5 --&gt; 0xA045.</p>
252 <p>The other method builds ub the number from the right. Let's try 41'029
253 again.  Divide by 16 and do not use fractions (only whole numbers).</p>
254 <pre>
255  41'029 / 16 is 2'564 with a remainder of 5. Write down 5.
256  2'564 / 16 is 160 with a remainder of 4. Write the 4 before the 5.
257  160 / 16 is 10 with no remainder. Prepend 45 with 0.
258  10 / 16 is below one. End here and prepend 0xA. End up with 0xA045.</pre>
259 <p>Which method to use is up to you. Use whatever works for you.  I use
260 them both without being able to tell what method I use in each case,
261 it just depends on the number, I think. Fact is, some numbers will
262 occur frequently while programming. If the number is close to one I am
263 familiar with, then I will use the first method (like 32'770 which is
264 into 32'768 + 2 and I just know that it is 0x8000 + 0x2 = 0x8002).</p>
265 <p>For binary the same approach can be used. The base is 2 and not 16,
266 and the number of positions will grow rapidly. Using the second method
267 has the advantage that you can see very easily if you should write down
268 a zero or a one: if you divide by two the remainder will be zero if it
269 is an even number and one if it is an odd number:</p>
270 <pre>
271  41029 / 2 = 20514 remainder 1
272  20514 / 2 = 10257 remainder 0
273  10257 / 2 =  5128 remainder 1
274   5128 / 2 =  2564 remainder 0
275   2564 / 2 =  1282 remainder 0
276   1282 / 2 =   641 remainder 0
277    641 / 2 =   320 remainder 1
278    320 / 2 =   160 remainder 0
279    160 / 2 =    80 remainder 0
280     80 / 2 =    40 remainder 0
281     40 / 2 =    20 remainder 0
282     20 / 2 =    10 remainder 0
283     10 / 2 =     5 remainder 0
284      5 / 2 =     2 remainder 1
285      2 / 2 =     1 remainder 0
286      1 / 2 below 0 remainder 1</pre>
287 <p>Write down the results from right to left: %1010000001000101</p>
288 <p>Group by four:</p>
289 <pre>
290  %1010000001000101
291  %101000000100 0101
292  %10100000 0100 0101
293  %1010 0000 0100 0101</pre>
294 <p>Convert into hexadecimal: 0xA045</p>
295 <p>Group %1010000001000101 by three and convert into octal:</p>
296 <pre>
297  %1010000001000101
298  %1010000001000 101
299  %1010000001 000 101
300  %1010000 001 000 101
301  %1010 000 001 000 101
302  %1 010 000 001 000 101
303  %001 010 000 001 000 101
304     1   2   0   1   0   5 --&gt; 0120105</pre>
305 <pre>
306  So: %1010000001000101 = 0120105 = 0xA045 = 41029
307  Or: 1010000001000101(2) = 120105(8) = A045(16) = 41029(10)
308  Or: 1010000001000101(2) = 120105(8) = A045(16) = 41029</pre>
309 <p>At first while adding numbers, you'll convert them to their decimal
310 form and then back into their original form after doing the addition.
311 If you use the other numbering system often, you will see that you'll
312 be able to do arithmetics directly in the base that is used.
313 In any representation it is the same, add the numbers on the right,
314 write down the right-most digit from the result, remember the other
315 digits and use them in the next round. Continue with the second digit
316 from the right and so on:</p>
317 <pre>
318     %1010 + %0111 --&gt; 10 + 7 --&gt; 17 --&gt; %00010001</pre>
319 <p>will become</p>
320 <pre>
321     %1010
322     %0111 +
323      ||||
324      |||+-- add 0 + 1, result is 1, nothing to remember
325      ||+--- add 1 + 1, result is %10, write down 0 and remember 1
326      |+---- add 0 + 1 + 1(remembered), result = 0, remember 1
327      +----- add 1 + 0 + 1(remembered), result = 0, remember 1
328             nothing to add, 1 remembered, result = 1
329  --------
330    %10001 is the result, I like to write it as %00010001</pre>
331 <p>For low values, try to do the calculations yourself, then check them with
332 a calculator. The more you do the calculations yourself, the more you'll
333 find that you didn't make mistakes. In the end, you'll do calculi in
334 other bases as easily as you do them in decimal.</p>
335 <p>When the numbers get bigger, you'll have to realize that a computer is
336 not called a computer just to have a nice name. There are many
337 different calculators available, use them. For Unix you could use ``bc''
338 which is short for Binary Calculator. It calculates not only in
339 decimal, but in all bases you'll ever want to use (among them Binary).</p>
340 <p>For people on Windows:
341 Start the calculator (start-&gt;programs-&gt;accessories-&gt;calculator)
342 and if necessary click view-&gt;scientific. You now have a scientific
343 calculator and can compute in binary or hexadecimal.</p>
344 <p>
345 </p>
346 <hr />
347 <h1><a name="author">AUTHOR</a></h1>
348 <p>I hope you enjoyed the examples and their descriptions. If you do, help
349 other people by pointing them to this document when they are asking
350 basic questions. They will not only get their answer, but at the same
351 time learn a whole lot more.</p>
352 <p>Alex van den Bogaerdt  &lt;<a href="mailto:alex@ergens.op.het.net">alex@ergens.op.het.net</a>&gt;</p>
354 </body>
356 </html>