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