Code

f6aef5be917be0ddf121fa7591e19d043bacbc20
[pkg-rrdtool.git] / doc / bin_dec_hex.1
1 .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.14
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.  | will give a
29 .\" real vertical bar.  \*(C+ will give a nicer C++.  Capital omega is used to
30 .\" do unbreakable dashes and therefore won't be available.  \*(C` and \*(C'
31 .\" expand to `' in nroff, nothing in troff, for use with C<>.
32 .tr \(*W-|\(bv\*(Tr
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 "2009-02-21" "1.3.99909060808" "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
304 representation (base) is different: 021 0x11 17 \f(CW%00010001\fR
305 .PP
306 To do arithmetics and conversions you need to understand one more thing.
307 It is something you already know but perhaps you do not \*(L"see\*(R" it yet:
308 .PP
309 If you write down 1234, (no prefix, so it is decimal) you are talking
310 about the number one thousand, two hundred and thirty four. In sort of
311 a formula:
312 .PP
313 .Vb 4
314 \& 1 * 1000 = 1000
315 \& 2 *  100 =  200
316 \& 3 *   10 =   30
317 \& 4 *    1 =    4
318 .Ve
319 .PP
320 This can also be written as:
321 .PP
322 .Vb 4
323 \& 1 * 10^3
324 \& 2 * 10^2
325 \& 3 * 10^1
326 \& 4 * 10^0
327 .Ve
328 .PP
329 where ^ means \*(L"to the power of\*(R".
330 .PP
331 We are using the base 10, and the positions 0,1,2 and 3.
332 The right-most position should \s-1NOT\s0 be multiplied with 10. The second
333 from the right should be multiplied one time with 10. The third from
334 the right is multiplied with 10 two times. This continues for whatever
335 positions are used.
336 .PP
337 It is the same in all other representations:
338 .PP
339 0x1234 will be
340 .PP
341 .Vb 4
342 \& 1 * 16^3
343 \& 2 * 16^2
344 \& 3 * 16^1
345 \& 4 * 16^0
346 .Ve
347 .PP
348 01234 would be
349 .PP
350 .Vb 4
351 \& 1 * 8^3
352 \& 2 * 8^2
353 \& 3 * 8^1
354 \& 4 * 8^0
355 .Ve
356 .PP
357 This example can not be done for binary as that system only uses two
358 symbols. Another example:
359 .PP
360 %1010 would be
361 .PP
362 .Vb 4
363 \& 1 * 2^3
364 \& 0 * 2^2
365 \& 1 * 2^1
366 \& 0 * 2^0
367 .Ve
368 .PP
369 It would have been easier to convert it to its hexadecimal form and
370 just translate \f(CW%1010\fR into 0xA. After a while you get used to it. You will
371 not need to do any calculations anymore, but just know that 0xA means 10.
372 .PP
373 To convert a decimal number into a hexadecimal you could use the next
374 method. It will take some time to be able to do the estimates, but it
375 will be easier when you use the system more frequently. We'll look at
376 yet another way afterwards.
377 .PP
378 First you need to know how many positions will be used in the other
379 system. To do so, you need to know the maximum numbers you'll be
380 using. Well, that's not as hard as it looks. In decimal, the maximum
381 number that you can form with two digits is \*(L"99\*(R". The maximum for
382 three: \*(L"999\*(R". The next number would need an extra position. Reverse
383 this idea and you will see that the number can be found by taking 10^3
384 (10*10*10 is 1000) minus 1 or 10^2 minus one.
385 .PP
386 This can be done for hexadecimal as well:
387 .PP
388 .Vb 4
389 \& 16^4 = 0x10000 = 65536
390 \& 16^3 =  0x1000 =  4096
391 \& 16^2 =   0x100 =   256
392 \& 16^1 =    0x10 =    16
393 .Ve
394 .PP
395 If a number is smaller than 65'536 it will fit in four positions.
396 If the number is bigger than 4'095, you must use position 4.
397 How many times you can subtract 4'096 from the number without going below
398 zero is the first digit you write down. This will always be a number
399 from 1 to 15 (0x1 to 0xF). Do the same for the other positions.
400 .PP
401 Let's try with 41'029. It is smaller than 16^4 but bigger than 16^3\-1. This
402 means that we have to use four positions.
403 We can subtract 16^3 from 41'029 ten times without going below zero.
404 The left-most digit will therefore be \*(L"A\*(R", so we have 0xA????.
405 The number is reduced to 41'029 \- 10*4'096 = 41'029\-40'960 = 69.
406 69 is smaller than 16^3 but not bigger than 16^2\-1. The second digit
407 is therefore \*(L"0\*(R" and we now have 0xA0??.
408 69 is smaller than 16^2 and bigger than 16^1\-1. We can subtract 16^1
409 (which is just plain 16) four times and write down \*(L"4\*(R" to get 0xA04?.
410 Subtract 64 from 69 (69 \- 4*16) and the last digit is 5 \-\-> 0xA045.
411 .PP
412 The other method builds up the number from the right. Let's try 41'029
413 again.  Divide by 16 and do not use fractions (only whole numbers).
414 .PP
415 .Vb 4
416 \& 41'029 / 16 is 2'564 with a remainder of 5. Write down 5.
417 \& 2'564 / 16 is 160 with a remainder of 4. Write the 4 before the 5.
418 \& 160 / 16 is 10 with no remainder. Prepend 45 with 0.
419 \& 10 / 16 is below one. End here and prepend 0xA. End up with 0xA045.
420 .Ve
421 .PP
422 Which method to use is up to you. Use whatever works for you.  I use
423 them both without being able to tell what method I use in each case,
424 it just depends on the number, I think. Fact is, some numbers will
425 occur frequently while programming. If the number is close to one I am
426 familiar with, then I will use the first method (like 32'770 which is
427 into 32'768 + 2 and I just know that it is 0x8000 + 0x2 = 0x8002).
428 .PP
429 For binary the same approach can be used. The base is 2 and not 16,
430 and the number of positions will grow rapidly. Using the second method
431 has the advantage that you can see very easily if you should write down
432 a zero or a one: if you divide by two the remainder will be zero if it
433 is an even number and one if it is an odd number:
434 .PP
435 .Vb 16
436 \& 41029 / 2 = 20514 remainder 1
437 \& 20514 / 2 = 10257 remainder 0
438 \& 10257 / 2 =  5128 remainder 1
439 \&  5128 / 2 =  2564 remainder 0
440 \&  2564 / 2 =  1282 remainder 0
441 \&  1282 / 2 =   641 remainder 0
442 \&   641 / 2 =   320 remainder 1
443 \&   320 / 2 =   160 remainder 0
444 \&   160 / 2 =    80 remainder 0
445 \&    80 / 2 =    40 remainder 0
446 \&    40 / 2 =    20 remainder 0
447 \&    20 / 2 =    10 remainder 0
448 \&    10 / 2 =     5 remainder 0
449 \&     5 / 2 =     2 remainder 1
450 \&     2 / 2 =     1 remainder 0
451 \&     1 / 2 below 0 remainder 1
452 .Ve
453 .PP
454 Write down the results from right to left: \f(CW%1010000001000101\fR
455 .PP
456 Group by four:
457 .PP
458 .Vb 4
459 \& %1010000001000101
460 \& %101000000100 0101
461 \& %10100000 0100 0101
462 \& %1010 0000 0100 0101
463 .Ve
464 .PP
465 Convert into hexadecimal: 0xA045
466 .PP
467 Group \f(CW%1010000001000101\fR by three and convert into octal:
468 .PP
469 .Vb 8
470 \& %1010000001000101
471 \& %1010000001000 101
472 \& %1010000001 000 101
473 \& %1010000 001 000 101
474 \& %1010 000 001 000 101
475 \& %1 010 000 001 000 101
476 \& %001 010 000 001 000 101
477 \&    1   2   0   1   0   5 \-\-> 0120105
478 .Ve
479 .PP
480 .Vb 3
481 \& So: %1010000001000101 = 0120105 = 0xA045 = 41029
482 \& Or: 1010000001000101(2) = 120105(8) = A045(16) = 41029(10)
483 \& Or: 1010000001000101(2) = 120105(8) = A045(16) = 41029
484 .Ve
485 .PP
486 At first while adding numbers, you'll convert them to their decimal
487 form and then back into their original form after doing the addition.
488 If you use the other numbering system often, you will see that you'll
489 be able to do arithmetics directly in the base that is used.
490 In any representation it is the same, add the numbers on the right,
491 write down the right-most digit from the result, remember the other
492 digits and use them in the next round. Continue with the second digit
493 from the right and so on:
494 .PP
495 .Vb 1
496 \&    %1010 + %0111 \-\-> 10 + 7 \-\-> 17 \-\-> %00010001
497 .Ve
498 .PP
499 will become
500 .PP
501 .Vb 10
502 \&    %1010
503 \&    %0111 +
504 \&     ||||
505 \&     |||+\-\- add 0 + 1, result is 1, nothing to remember
506 \&     ||+\-\-\- add 1 + 1, result is %10, write down 0 and remember 1
507 \&     |+\-\-\-\- add 0 + 1 + 1(remembered), result = 0, remember 1
508 \&     +\-\-\-\-\- add 1 + 0 + 1(remembered), result = 0, remember 1
509 \&            nothing to add, 1 remembered, result = 1
510 \& \-\-\-\-\-\-\-\-
511 \&   %10001 is the result, I like to write it as %00010001
512 .Ve
513 .PP
514 For low values, try to do the calculations yourself, then check them with
515 a calculator. The more you do the calculations yourself, the more you'll
516 find that you didn't make mistakes. In the end, you'll do calculi in
517 other bases as easily as you do them in decimal.
518 .PP
519 When the numbers get bigger, you'll have to realize that a computer is
520 not called a computer just to have a nice name. There are many
521 different calculators available, use them. For Unix you could use \*(L"bc\*(R"
522 which is short for Binary Calculator. It calculates not only in
523 decimal, but in all bases you'll ever want to use (among them Binary).
524 .PP
525 For people on Windows:
526 Start the calculator (start\->programs\->accessories\->calculator)
527 and if necessary click view\->scientific. You now have a scientific
528 calculator and can compute in binary or hexadecimal.
529 .SH "AUTHOR"
530 .IX Header "AUTHOR"
531 I hope you enjoyed the examples and their descriptions. If you do, help
532 other people by pointing them to this document when they are asking
533 basic questions. They will not only get their answer, but at the same
534 time learn a whole lot more.
535 .PP
536 Alex van den Bogaerdt  <alex@vandenbogaerdt.nl>