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