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