Code

Imported upstream version 1.2.26
[pkg-rrdtool.git] / doc / bin_dec_hex.1
1 .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32
2 .\"
3 .\" Standard preamble:
4 .\" ========================================================================
5 .de Sh \" Subsection heading
6 .br
7 .if t .Sp
8 .ne 5
9 .PP
10 \fB\\$1\fR
11 .PP
12 ..
13 .de Sp \" Vertical space (when we can't use .PP)
14 .if t .sp .5v
15 .if n .sp
16 ..
17 .de Vb \" Begin verbatim text
18 .ft CW
19 .nf
20 .ne \\$1
21 ..
22 .de Ve \" End verbatim text
23 .ft R
24 .fi
25 ..
26 .\" Set up some character translations and predefined strings.  \*(-- will
27 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28 .\" double quote, and \*(R" will give a right double quote.  \*(C+ will
29 .\" give a nicer C++.  Capital omega is used to do unbreakable dashes and
30 .\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,
31 .\" nothing in troff, for use with C<>.
32 .tr \(*W-
33 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34 .ie n \{\
35 .    ds -- \(*W-
36 .    ds PI pi
37 .    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38 .    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
39 .    ds L" ""
40 .    ds R" ""
41 .    ds C` ""
42 .    ds C' ""
43 'br\}
44 .el\{\
45 .    ds -- \|\(em\|
46 .    ds PI \(*p
47 .    ds L" ``
48 .    ds R" ''
49 'br\}
50 .\"
51 .\" If the F register is turned on, we'll generate index entries on stderr for
52 .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53 .\" entries marked with X<> in POD.  Of course, you'll have to process the
54 .\" output yourself in some meaningful fashion.
55 .if \nF \{\
56 .    de IX
57 .    tm Index:\\$1\t\\n%\t"\\$2"
58 ..
59 .    nr % 0
60 .    rr F
61 .\}
62 .\"
63 .\" For nroff, turn off justification.  Always turn off hyphenation; it makes
64 .\" way too many mistakes in technical documents.
65 .hy 0
66 .if n .na
67 .\"
68 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69 .\" Fear.  Run.  Save yourself.  No user-serviceable parts.
70 .    \" fudge factors for nroff and troff
71 .if n \{\
72 .    ds #H 0
73 .    ds #V .8m
74 .    ds #F .3m
75 .    ds #[ \f1
76 .    ds #] \fP
77 .\}
78 .if t \{\
79 .    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80 .    ds #V .6m
81 .    ds #F 0
82 .    ds #[ \&
83 .    ds #] \&
84 .\}
85 .    \" simple accents for nroff and troff
86 .if n \{\
87 .    ds ' \&
88 .    ds ` \&
89 .    ds ^ \&
90 .    ds , \&
91 .    ds ~ ~
92 .    ds /
93 .\}
94 .if t \{\
95 .    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96 .    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97 .    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98 .    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99 .    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100 .    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101 .\}
102 .    \" troff and (daisy-wheel) nroff accents
103 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110 .ds ae a\h'-(\w'a'u*4/10)'e
111 .ds Ae A\h'-(\w'A'u*4/10)'E
112 .    \" corrections for vroff
113 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115 .    \" for low resolution devices (crt and lpr)
116 .if \n(.H>23 .if \n(.V>19 \
117 \{\
118 .    ds : e
119 .    ds 8 ss
120 .    ds o a
121 .    ds d- d\h'-1'\(ga
122 .    ds D- D\h'-1'\(hy
123 .    ds th \o'bp'
124 .    ds Th \o'LP'
125 .    ds ae ae
126 .    ds Ae AE
127 .\}
128 .rm #[ #] #H #V #F C
129 .\" ========================================================================
130 .\"
131 .IX Title "BIN_DEC_HEX 1"
132 .TH BIN_DEC_HEX 1 "2007-11-20" "1.2.26" "rrdtool"
133 .SH "NAME"
134 bin_dec_hex \- How to use binary, decimal, and hexadecimal notation.
135 .SH "DESCRIPTION"
136 .IX Header "DESCRIPTION"
137 Most people use the decimal numbering system. This system uses ten
138 symbols to represent numbers. When those ten symbols are used up, they
139 start all over again and increment the position to the left. The
140 digit 0 is only shown if it is the only symbol in the sequence, or if
141 it is not the first one.
142 .PP
143 If this sounds cryptic to you, this is what I've just said in numbers:
144 .PP
145 .Vb 14
146 \&     0
147 \&     1
148 \&     2
149 \&     3
150 \&     4
151 \&     5
152 \&     6
153 \&     7
154 \&     8
155 \&     9
156 \&    10
157 \&    11
158 \&    12
159 \&    13
160 .Ve
161 .PP
162 and so on.
163 .PP
164 Each time the digit nine is incremented, it is reset to 0 and the
165 position before (to the left) is incremented (from 0 to 1). Then
166 number 9 can be seen as \*(L"00009\*(R" and when we should increment 9, we
167 reset it to zero and increment the digit just before the 9 so the
168 number becomes \*(L"00010\*(R". Leading zeros we don't write except if it is
169 the only digit (number 0). And of course, we write zeros if they occur
170 anywhere inside or at the end of a number:
171 .PP
172 .Vb 1
173 \& "00010" \-> " 0010" \-> " 010" \-> "  10", but not "  1 ".
174 .Ve
175 .PP
176 This was pretty basic, you already knew this. Why did I tell it?
177 Well, computers usually do not represent numbers with 10 different
178 digits. They only use two different symbols, namely \*(L"0\*(R" and \*(L"1\*(R". Apply
179 the same rules to this set of digits and you get the binary numbering
180 system:
181 .PP
182 .Vb 14
183 \&     0
184 \&     1
185 \&    10
186 \&    11
187 \&   100
188 \&   101
189 \&   110
190 \&   111
191 \&  1000
192 \&  1001
193 \&  1010
194 \&  1011
195 \&  1100
196 \&  1101
197 .Ve
198 .PP
199 and so on.
200 .PP
201 If you count the number of rows, you'll see that these are again 14
202 different numbers. The numbers are the same and mean the same as in
203 the first list, we just used a different representation. This means
204 that you have to know the representation used, or as it is called the
205 numbering system or base.  Normally, if we do not explicitly specify
206 the numbering system used, we implicitly use the decimal system. If we
207 want to use any other numbering system, we'll have to make that
208 clear. There are a few widely adopted methods to do so. One common
209 form is to write 1010(2) which means that you wrote down a number in
210 its binary representation. It is the number ten. If you would write
211 1010 without specifying the base, the number is interpreted as one
212 thousand and ten using base 10.
213 .PP
214 In books, another form is common. It uses subscripts (little
215 characters, more or less in between two rows). You can leave out the
216 parentheses in that case and write down the number in normal
217 characters followed by a little two just behind it.
218 .PP
219 As the numbering system used is also called the base, we talk of the
220 number 1100 base 2, the number 12 base 10.
221 .PP
222 Within the binary system, it is common to write leading zeros. The
223 numbers are written down in series of four, eight or sixteen depending
224 on the context.
225 .PP
226 We can use the binary form when talking to computers
227 (...programming...), but the numbers will have large
228 representations. The number 65'535 (often in the decimal system a ' is
229 used to separate blocks of three digits for readability) would be
230 written down as 1111111111111111(2) which is 16 times the digit 1.
231 This is difficult and prone to errors. Therefore, we usually would use
232 another base, called hexadecimal. It uses 16 different symbols. First
233 the symbols from the decimal system are used, thereafter we continue
234 with alphabetic characters. We get 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
235 A, B, C, D, E and F. This system is chosen because the hexadecimal
236 form can be converted into the binary system very easily (and back).
237 .PP
238 There is yet another system in use, called the octal system. This was
239 more common in the old days, but is not used very often anymore. As
240 you might find it in use sometimes, you should get used to it and
241 we'll show it below. It's the same story as with the other
242 representations, but with eight different symbols.
243 .PP
244 .Vb 4
245 \& Binary      (2)
246 \& Octal       (8)
247 \& Decimal     (10)
248 \& Hexadecimal (16)
249 .Ve
250 .PP
251 .Vb 23
252 \& (2)    (8) (10) (16)
253 \& 00000   0    0    0
254 \& 00001   1    1    1
255 \& 00010   2    2    2
256 \& 00011   3    3    3
257 \& 00100   4    4    4
258 \& 00101   5    5    5
259 \& 00110   6    6    6
260 \& 00111   7    7    7
261 \& 01000  10    8    8
262 \& 01001  11    9    9
263 \& 01010  12   10    A
264 \& 01011  13   11    B
265 \& 01100  14   12    C
266 \& 01101  15   13    D
267 \& 01110  16   14    E
268 \& 01111  17   15    F
269 \& 10000  20   16   10
270 \& 10001  21   17   11
271 \& 10010  22   18   12
272 \& 10011  23   19   13
273 \& 10100  24   20   14
274 \& 10101  25   21   15
275 .Ve
276 .PP
277 Most computers used nowadays are using bytes of eight bits. This means
278 that they store eight bits at a time. You can see why the octal system
279 is not the most practical for that: You'd need three digits to represent
280 the eight bits and this means that you'd have to use one complete digit
281 to represent only two bits (2+3+3=8). This is a waste. For hexadecimal
282 digits, you need only two digits which are used completely:
283 .PP
284 .Vb 2
285 \& (2)      (8)  (10) (16)
286 \& 11111111 377  255   FF
287 .Ve
288 .PP
289 You can see why binary and hexadecimal can be converted quickly: For
290 each hexadecimal digit there are exactly four binary digits.  Take a
291 binary number: take four digits from the right and make a hexadecimal
292 digit from it (see the table above). Repeat this until there are no
293 more digits. And the other way around: Take a hexadecimal number. For
294 each digit, write down its binary equivalent.
295 .PP
296 Computers (or rather the parsers running on them) would have a hard
297 time converting a number like 1234(16). Therefore hexadecimal numbers
298 are specified with a prefix. This prefix depends on the language
299 you're writing in. Some of the prefixes are \*(L"0x\*(R" for C, \*(L"$\*(R" for
300 Pascal, \*(L"#\*(R" for \s-1HTML\s0.  It is common to assume that if a number starts
301 with a zero, it is octal. It does not matter what is used as long as
302 you know what it is. I will use \*(L"0x\*(R" for hexadecimal, \*(L"%\*(R" for binary
303 and \*(L"0\*(R" for octal.  The following numbers are all the same, just their represenatation (base) is different: 021 0x11 17 \f(CW%00010001\fR
304 .PP
305 To do arithmetics and conversions you need to understand one more thing.
306 It is something you already know but perhaps you do not \*(L"see\*(R" it yet:
307 .PP
308 If you write down 1234, (no prefix, so it is decimal) you are talking
309 about the number one thousand, two hundred and thirty four. In sort of
310 a formula:
311 .PP
312 .Vb 4
313 \& 1 * 1000 = 1000
314 \& 2 *  100 =  200
315 \& 3 *   10 =   30
316 \& 4 *    1 =    4
317 .Ve
318 .PP
319 This can also be written as:
320 .PP
321 .Vb 4
322 \& 1 * 10^3
323 \& 2 * 10^2
324 \& 3 * 10^1
325 \& 4 * 10^0
326 .Ve
327 .PP
328 where ^ means \*(L"to the power of\*(R".
329 .PP
330 We are using the base 10, and the positions 0,1,2 and 3.
331 The right-most position should \s-1NOT\s0 be multiplied with 10. The second
332 from the right should be multiplied one time with 10. The third from
333 the right is multiplied with 10 two times. This continues for whatever
334 positions are used.
335 .PP
336 It is the same in all other representations:
337 .PP
338 0x1234 will be
339 .PP
340 .Vb 4
341 \& 1 * 16^3
342 \& 2 * 16^2
343 \& 3 * 16^1
344 \& 4 * 16^0
345 .Ve
346 .PP
347 01234 would be
348 .PP
349 .Vb 4
350 \& 1 * 8^3
351 \& 2 * 8^2
352 \& 3 * 8^1
353 \& 4 * 8^0
354 .Ve
355 .PP
356 This example can not be done for binary as that system only uses two
357 symbols. Another example:
358 .PP
359 %1010 would be
360 .PP
361 .Vb 4
362 \& 1 * 2^3
363 \& 0 * 2^2
364 \& 1 * 2^1
365 \& 0 * 2^0
366 .Ve
367 .PP
368 It would have been easier to convert it to its hexadecimal form and
369 just translate \f(CW%1010\fR into 0xA. After a while you get used to it. You will
370 not need to do any calculations anymore, but just know that 0xA means 10.
371 .PP
372 To convert a decimal number into a hexadecimal you could use the next
373 method. It will take some time to be able to do the estimates, but it
374 will be easier when you use the system more frequently. We'll look at
375 yet another way afterwards.
376 .PP
377 First you need to know how many positions will be used in the other
378 system. To do so, you need to know the maximum numbers you'll be
379 using. Well, that's not as hard as it looks. In decimal, the maximum
380 number that you can form with two digits is \*(L"99\*(R". The maximum for
381 three: \*(L"999\*(R". The next number would need an extra position. Reverse
382 this idea and you will see that the number can be found by taking 10^3
383 (10*10*10 is 1000) minus 1 or 10^2 minus one.
384 .PP
385 This can be done for hexadecimal as well:
386 .PP
387 .Vb 4
388 \& 16^4 = 0x10000 = 65536
389 \& 16^3 =  0x1000 =  4096
390 \& 16^2 =   0x100 =   256
391 \& 16^1 =    0x10 =    16
392 .Ve
393 .PP
394 If a number is smaller than 65'536 it will fit in four positions.
395 If the number is bigger than 4'095, you must use position 4.
396 How many times you can subtract 4'096 from the number without going below
397 zero is the first digit you write down. This will always be a number
398 from 1 to 15 (0x1 to 0xF). Do the same for the other positions.
399 .PP
400 Let's try with 41'029. It is smaller than 16^4 but bigger than 16^3\-1. This
401 means that we have to use four positions.
402 We can subtract 16^3 from 41'029 ten times without going below zero.
403 The left-most digit will therefore be \*(L"A\*(R", so we have 0xA????.
404 The number is reduced to 41'029 \- 10*4'096 = 41'029\-40'960 = 69.
405 69 is smaller than 16^3 but not bigger than 16^2\-1. The second digit
406 is therefore \*(L"0\*(R" and we now have 0xA0??.
407 69 is smaller than 16^2 and bigger than 16^1\-1. We can subtract 16^1
408 (which is just plain 16) four times and write down \*(L"4\*(R" to get 0xA04?.
409 Subtract 64 from 69 (69 \- 4*16) and the last digit is 5 \-\-> 0xA045.
410 .PP
411 The other method builds ub the number from the right. Let's try 41'029
412 again.  Divide by 16 and do not use fractions (only whole numbers).
413 .PP
414 .Vb 4
415 \& 41'029 / 16 is 2'564 with a remainder of 5. Write down 5.
416 \& 2'564 / 16 is 160 with a remainder of 4. Write the 4 before the 5.
417 \& 160 / 16 is 10 with no remainder. Prepend 45 with 0.
418 \& 10 / 16 is below one. End here and prepend 0xA. End up with 0xA045.
419 .Ve
420 .PP
421 Which method to use is up to you. Use whatever works for you.  I use
422 them both without being able to tell what method I use in each case,
423 it just depends on the number, I think. Fact is, some numbers will
424 occur frequently while programming. If the number is close to one I am
425 familiar with, then I will use the first method (like 32'770 which is
426 into 32'768 + 2 and I just know that it is 0x8000 + 0x2 = 0x8002).
427 .PP
428 For binary the same approach can be used. The base is 2 and not 16,
429 and the number of positions will grow rapidly. Using the second method
430 has the advantage that you can see very easily if you should write down
431 a zero or a one: if you divide by two the remainder will be zero if it
432 is an even number and one if it is an odd number:
433 .PP
434 .Vb 16
435 \& 41029 / 2 = 20514 remainder 1
436 \& 20514 / 2 = 10257 remainder 0
437 \& 10257 / 2 =  5128 remainder 1
438 \&  5128 / 2 =  2564 remainder 0
439 \&  2564 / 2 =  1282 remainder 0
440 \&  1282 / 2 =   641 remainder 0
441 \&   641 / 2 =   320 remainder 1
442 \&   320 / 2 =   160 remainder 0
443 \&   160 / 2 =    80 remainder 0
444 \&    80 / 2 =    40 remainder 0
445 \&    40 / 2 =    20 remainder 0
446 \&    20 / 2 =    10 remainder 0
447 \&    10 / 2 =     5 remainder 0
448 \&     5 / 2 =     2 remainder 1
449 \&     2 / 2 =     1 remainder 0
450 \&     1 / 2 below 0 remainder 1
451 .Ve
452 .PP
453 Write down the results from right to left: \f(CW%1010000001000101\fR
454 .PP
455 Group by four:
456 .PP
457 .Vb 4
458 \& %1010000001000101
459 \& %101000000100 0101
460 \& %10100000 0100 0101
461 \& %1010 0000 0100 0101
462 .Ve
463 .PP
464 Convert into hexadecimal: 0xA045
465 .PP
466 Group \f(CW%1010000001000101\fR by three and convert into octal:
467 .PP
468 .Vb 8
469 \& %1010000001000101
470 \& %1010000001000 101
471 \& %1010000001 000 101
472 \& %1010000 001 000 101
473 \& %1010 000 001 000 101
474 \& %1 010 000 001 000 101
475 \& %001 010 000 001 000 101
476 \&    1   2   0   1   0   5 \-\-> 0120105
477 .Ve
478 .PP
479 .Vb 3
480 \& So: %1010000001000101 = 0120105 = 0xA045 = 41029
481 \& Or: 1010000001000101(2) = 120105(8) = A045(16) = 41029(10)
482 \& Or: 1010000001000101(2) = 120105(8) = A045(16) = 41029
483 .Ve
484 .PP
485 At first while adding numbers, you'll convert them to their decimal
486 form and then back into their original form after doing the addition.
487 If you use the other numbering system often, you will see that you'll
488 be able to do arithmetics directly in the base that is used.
489 In any representation it is the same, add the numbers on the right,
490 write down the right-most digit from the result, remember the other
491 digits and use them in the next round. Continue with the second digit
492 from the right and so on:
493 .PP
494 .Vb 1
495 \&    %1010 + %0111 \-\-> 10 + 7 \-\-> 17 \-\-> %00010001
496 .Ve
497 .PP
498 will become
499 .PP
500 .Vb 10
501 \&    %1010
502 \&    %0111 +
503 \&     ||||
504 \&     |||+\-\- add 0 + 1, result is 1, nothing to remember
505 \&     ||+\-\-\- add 1 + 1, result is %10, write down 0 and remember 1
506 \&     |+\-\-\-\- add 0 + 1 + 1(remembered), result = 0, remember 1
507 \&     +\-\-\-\-\- add 1 + 0 + 1(remembered), result = 0, remember 1
508 \&            nothing to add, 1 remembered, result = 1
509 \& \-\-\-\-\-\-\-\-
510 \&   %10001 is the result, I like to write it as %00010001
511 .Ve
512 .PP
513 For low values, try to do the calculations yourself, then check them with
514 a calculator. The more you do the calculations yourself, the more you'll
515 find that you didn't make mistakes. In the end, you'll do calculi in
516 other bases as easily as you do them in decimal.
517 .PP
518 When the numbers get bigger, you'll have to realize that a computer is
519 not called a computer just to have a nice name. There are many
520 different calculators available, use them. For Unix you could use \*(L"bc\*(R"
521 which is short for Binary Calculator. It calculates not only in
522 decimal, but in all bases you'll ever want to use (among them Binary).
523 .PP
524 For people on Windows:
525 Start the calculator (start\->programs\->accessories\->calculator)
526 and if necessary click view\->scientific. You now have a scientific
527 calculator and can compute in binary or hexadecimal.
528 .SH "AUTHOR"
529 .IX Header "AUTHOR"
530 I hope you enjoyed the examples and their descriptions. If you do, help
531 other people by pointing them to this document when they are asking
532 basic questions. They will not only get their answer, but at the same
533 time learn a whole lot more.
534 .PP
535 Alex van den Bogaerdt  <alex@ergens.op.het.net>