3 Binary Decimal Hexadecimal - How does it work
5 =for html <div align="right"><a href="bin_dec_hex.pdf">PDF</a> version.</div>
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 just before this. 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.
15 If this sounds as crypto to you, this is what I've said in numbers:
34 Each time the digit nine should be incremented, it is reset to 0 and the
35 position before is incremented. Then number 9 can be seen as "00009" and
36 when we should increment 9, we reset it to zero and increment the digit
37 just before the 9 so the number becomes "00010". For zero's we write a
38 space if it is not the only digit (so: number 0) and if it is the first
39 digit: "00010" -> " 0010" -> " 010" -> " 10". It is not " 1 ".
41 This was pretty basic, you already knew this. Why did I tell it ?
42 Well, computers do not represent numbers with 10 different digits. They
43 know of only two different symbols, being 0 and 1. Apply the same rules
44 to this set of digits and you get the binary numbering system:
63 If you count the number of rows, you'll see that these are again 14
64 different numbers. The numbers are the same and mean the same. It is
65 only a different representation. This means that you have to know the
66 representation used, or as it is called the numbering system or base.
67 Normally if we do not speak about the numbering system used, we're
68 using the decimal system. If we are talking about another numbering
69 system, we'll have to make that clear. There are a few wide-spread
70 methods to do so. One common form is to write 1010(2) which means that
71 you wrote down a number in the binary form. It is the number ten.
72 If you would write 1010 it means the number one thousand and ten.
74 In books, another form is most used. It uses subscript (little chars,
75 more or less in between two rows). You can leave out the parentheses
76 in that case and write down the number in normal characters followed
77 with a little two just behind it.
79 The numbering system used is also called the base. We talk of the number
80 1100 base 2, the number 12 base 10.
82 For the binary system, is is common to write leading zero's. The numbers
83 are written down in series of four, eight or sixteen depending on the
86 We can use the binary form when talking to computers (...programming...)
87 but the numbers will have large representations. The number 65535 would
88 be written down as 1111111111111111(2) which is 16 times the digit 1.
89 This is difficult and prone to errors. Therefore we normally would use
90 another base, called hexadecimal. It uses 16 different symbols. First
91 the symbols from the decimal system are used, thereafter we continue
92 with the alphabetic characters. We get 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A,
93 B, C, D, E and F. This system is chosen because the hexadecimal form
94 can be converted into the binary system very easy (and back).
96 There is yet another system in use, called the octal system. This was
97 more common in the old days but not anymore. You will find it in use
98 on some places so get used to it. The same story applies, but now with
99 only eight different symbols.
130 Most computers used nowadays are using bytes of eight bits. This means
131 that they store eight bits at a time. You can see why the octal system
132 is not the most preferred for that: You'd need three digits to represent
133 the eight bits and this means that you'd have to use one complete digit
134 to represent only two bits (2+3+3=8). This is a waste. For hexadecimal
135 digits, you need only two digits which are used completely:
140 You can see why binary and hexadecimal can be converted quickly:
141 For each hexadecimal digit there are exactly four binary digits.
142 Take a binary number. Each time take four digits from the right and make
143 a hexadecimal digit from it (see the table above). Stop when there are
145 Other way around: Take a hexadecimal number. For each digit, write down
146 its binary equivalent.
148 Computers (or rather the parsers running on them) would have a hard time
149 converting a number like 1234(16). Therefore hexadecimal numbers get a
150 prefix. This prefix depends on the language you're writing in. Some of
151 the prefixes are "0x" for C, "$" for Pascal, "#" for HTML.
152 It is common to assume that if a number starts with a zero, it is octal.
153 It does not matter what is used as long as you know what it is.
154 I will use "0x" for hexadecimal, "%" for binary and "0" for octal.
155 The following numbers are all the same, just the way they are written is
156 different: 021 0x11 17 %00010001
158 To do arithmetics and conversions you need to understand one more thing.
159 It is something you already know but perhaps you do not "see" it yet:
161 If you write down 1234, (so it is decimal) you are talking about the
162 number one thousand, two hundred and thirty four. In sort of a formula:
169 This can also be written as:
176 where ^ means "to the power of".
178 We are using the base 10, and the positions 0,1,2 and 3.
179 The right-most position should NOT be multiplied with 10. The second
180 from the right should be multiplied one time with 10. The third from
181 the right is multiplied with 10 two times. This continues for whatever
184 It is the same in all other representations:
200 This example can not be done for binary as that system can only use two
201 symbols. Another example:
210 It would have been more easy to convert it to its hexadecimal form and
211 just translate %1010 into 0xA. After a while you get used to it. You will
212 not need to do any calculations anymore but just know that 0xA means 10.
214 To convert a decimal number into a hexadecimal one you could use the next
215 method. It will take some time to be able to do the estimates but it will
216 be more and more easy when you use the system more frequent. Another way
217 is presented to you thereafter.
219 First you will need to know how many positions will be used in the other
220 system. To do so, you need to know the maximum numbers. Well, that's not
221 so hard as it looks. In decimal, the maximum number that you can form
222 with two digits is "99". The maximum for three: "999". The next number
223 would need an extra position. Reverse this idea and you will see that
224 the number can be found by taking 10^3 (10*10*10 is 1000) minus 1 or
227 This can be done for hexadecimal too:
229 16^4 = 0x10000 = 65536
234 If a number is smaller than 65536 it will thus fit in four positions.
235 If the number is bigger than 4095, you will need to use position 4.
236 How many times can you take 4096 from the number without going below
237 zero is the first digit you write down. This will always be a number
238 from 1 to 15 (0x1 to 0xF). Do the same for the other positions.
240 Number is 41029. It is smaller than 16^4 but bigger than 16^3-1. This
241 means that we have to use four positions.
242 We can subtract 16^3 from 41029 ten times without going below zero.
243 The leftmost digit will be "A" so we have 0xA????.
244 The number is reduced to 41029 - 10*4096 = 41029-40960 = 69.
245 69 is smaller than 16^3 but not bigger than 16^2-1. The second digit
246 is therefore "0" and we know 0xA0??.
247 69 is smaller than 16^2 and bigger than 16^1-1. We can subtract 16^1
248 (which is just plain 16) four times and write down "4" to get 0xA04?.
249 Take 64 from 69 (69 - 4*16) and the last digit is 5 --> 0xA045.
251 The other method builds the number from the right. Take again 41029.
252 Divide by 16 and do not use fractions (only whole numbers).
254 41029 / 16 is 2564 with a remainder of 5. Write down 5.
255 2564 / 16 is 160 with a remainder of 4. Write the 4 before the 5.
256 160 / 16 is 10 with no remainder. Prepend 45 with 0.
257 10 / 16 is below one. End here and prepend 0xA. End up with 0xA045.
259 Which method to use is up to you. Use whatever works for you. Personally
260 I use them both without being able to tell what method I use in each
261 case, it just depends on the number, I think. Fact is, some numbers
262 will occur frequently while programming, if the number is close then
263 I will use the first method (like 32770, translate into 32768 + 2 and
264 just know that it is 0x8000 + 0x2 = 0x8002).
267 For binary the same approach can be used. The base is 2 and not 16,
268 and the number of positions will grow rapidly. Using the second method
269 has the advantage that you can see very simple if you should write down
270 a zero or a one: if you divide by two the remainder will be zero if it
271 was an even number and one if it was an odd number:
273 41029 / 2 = 20514 remainder 1
274 20514 / 2 = 10257 remainder 0
275 10257 / 2 = 5128 remainder 1
276 5128 / 2 = 2564 remainder 0
277 2564 / 2 = 1282 remainder 0
278 1282 / 2 = 641 remainder 0
279 641 / 2 = 320 remainder 1
280 320 / 2 = 160 remainder 0
281 160 / 2 = 80 remainder 0
282 80 / 2 = 40 remainder 0
283 40 / 2 = 20 remainder 0
284 20 / 2 = 10 remainder 0
285 10 / 2 = 5 remainder 0
286 5 / 2 = 2 remainder 1
287 2 / 2 = 1 remainder 0
288 1 / 2 below 0 remainder 1
290 Write down the results from right to left: %1010000001000101
299 Convert into hexadecimal: 0xA045
301 Group %1010000001000101 by three and convert into octal:
307 %1010 000 001 000 101
308 %1 010 000 001 000 101
309 %001 010 000 001 000 101
310 1 2 0 1 0 5 --> 0120105
312 So: %1010000001000101 = 0120105 = 0xA045 = 41029
313 Or: 1010000001000101(2) = 120105(8) = A045(16) = 41029(10)
314 Or: 1010000001000101(2) = 120105(8) = A045(16) = 41029
317 At first while adding numbers, you'll convert them to their decimal
318 form and then back into their original form after doing the addition.
319 If you use the other numbering system often, you will see that you'll
320 be able to do arithmetics in the base that is used.
321 In any representation it is the same, add the numbers on the right,
322 write down the rightmost digit from the result, remember the other
323 digits and use them in the next round. Continue with the second digits
324 from the right and so on:
326 %1010 + %0111 --> 10 + 7 --> 17 --> %00010001
333 |||+-- add 0 + 1, result is 1, nothing to remember
334 ||+--- add 1 + 1, result is %10, write down 0 and remember 1
335 |+---- add 0 + 1 + 1(remembered), result = 0, remember 1
336 +----- add 1 + 0 + 1(remembered), result = 0, remember 1
337 nothing to add, 1 remembered, result = 1
339 %10001 is the result, I like to write it as %00010001
341 For low values, try to do the calculations yourself, check them with
342 a calculator. The more you do the calculations yourself, the more you
343 find that you didn't make mistakes. In the end, you'll do calculi in
344 other bases as easy as you do in decimal.
346 When the numbers get bigger, you'll have to realize that a computer is
347 not called a computer just to have a nice name. There are many different
348 calculators available. Use them. For Unix you could use "bc" which is
349 called so as it is short for Binary Calculator. It calculates not only
350 in decimal, but in all bases you'll ever use (among them Binary).
352 For people on Windows:
353 Start the calculator (start->programs->accessories->calculator)
354 and if necessary click view->scientific. You now have a scientific
355 calculator and can compute in binary or hexadecimal.
359 I hope you enjoyed the examples and their descriptions. If you do, help
360 other people by pointing them to this document when they are asking
361 basic questions. They will not only get their answer but at the same
362 time learn a whole lot more.
364 Alex van den Bogaerdt
365 <alex@ergens.op.het.net>