1BIN_DEC_HEX(1)                      rrdtool                     BIN_DEC_HEX(1)
2
3
4

NAME

6       bin_dec_hex - How to use binary, decimal, and hexadecimal notation.
7

DESCRIPTION

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

AUTHOR

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  <alex@vandenbogaerdt.nl>
373
374
375
3761.4.8                             2013-05-23                    BIN_DEC_HEX(1)
Impressum