The computer usage requires the capacity to convert numbers expressed in different number systems; to be precise, one can request to convert numbers on a different base. Before being able convert a number in a different number system, it is necessary the understand the meaning of the decimal number system i.e. the standard one (for the human beings).
A number expressed in base 10, is interpreted by summing up the value of each single digit multiplied by 10n (n rappresents the n-nth; digit starting from zero). For example, 12 345 may be expressed as 5*100 + 4*101 + 3*102 + 2*103 + 1*104. In a similar way, one can decompose a number to express it in base 10 by repeatedly dividing the number by the base and then using each time the result of the division. For example, the number 12 345 (which obviously is already expressed in base 10), can be decomposed as follows: 12 345/10=1 234 with the remiander five; 1 234/10=123 with the remainder three; 123/10=12 with the remainder three; 12/10=1 with the remainder two; 1/10=0 with the remainder one; (when the quotient becomes null, the conversion is finished). The sequence of the remainders is the number expressed in base 10: 12 345.
586.1
Octal Numbers
The octal number sysstem, i.e. the vase 8 numbering systems, uses eight digits to represent the values: from zero to seven. The conversion technique of an octal number to a decimal number is the same as that exemplified for the decimal numbers, with the difference that the numbers base is eigth. For example to interpret the octal number 123458, one should process it as follows: 5*80 + 4*81 + 3*82 + 2*83 + 1*84. The same number could therefore be represented in base 10 as 5 349. To convert the other way round the number 5 349 (now expressed in base 10), one can process it as follows: 5 349/8=668 with the remainder of five; 668/8=83 with the remainder of four; 83/8=10 with the remainder of three; 10/8=1 with the remainder of two; 1/8=0 with the remainder of one. Now we reobtain the octal number 123458.
32485 in base 10 --> | 32485/8 = 4060 with remainder of 5
| 4060/8 = 507 with remainder of 4
| 507/8 = 63 with remainder of 3
| 63/8 = 7 with remainder of 7
| 7/8 = 0 with remainder of 7
|
`---------------------------> 77345 in base 8
Image 586.2. Computation of the value corresponding to a value expressed in base eight.
77345 in base 8 --> | 5*8^0 = 5
| 4*8^1 = 32
| 3*8^2 = 192
| 7*8^3 = 3584
| 7*8^4 = 28672
|
`-- total = 32485 in base 10
586.2
Hexadecimal Numbers
The hexadecimal number system, i.e. the system in base 16, works in a similar way as the octal one, with the difference that uses 16 digits to reppresent the values, therefore it uses the numeric digits from zero to nine plus the letters from ``A'' to ``F'' for the larger values. In practice, the letter ``A'' is used for the number 10 and the letter ``F'' is used for the number 15.
The conversion technique is the same as that previously seen with the octal number system, with the additional difficulty of the additional letters. For example to interpret the hexadecimal number 19ADF16, one can proceed as follows: 15*160 + 13*161 + 10*162 + 9*163 + 1*164. Therefore the same number could be represented in base 10 as 105 183. The other way round, to convert the number 105 183 (here expressed in base 10), one can process it as follows: 105 183/16=6 573 with the remainder of 15, or F16; 6 573/16=410 with the remainder of 13, or D16; 410/16=25 with the remiander of 10, or A16; 25/16=1 with the remainder of nine; 1/16=0 with the remainder of one. In this way one gets back the hexadecimal number 19ADF16.
32485 in base 10 --> | 32485/16 = 2030 with the remainder of 5 --> 5
| 2030/16 = 126 with the remainder of 14 --> E
| 126/16 = 7 with the remainder of 14 --> E
| 7/16 = 0 with the remainder of 7 --> 7
|
`-----------------------------------> 7EE5 in base 16
Figura 586.4. Computation of the value corresponding to a number expressed in base 16.
7EE5 in base 16 --> | 5*16^0 = 5
| E --> 14*16^1 = 224
| E --> 14*16^2 = 3584
| 7*16^3 = 28672
|
`------- total = 32485 in base 10
586.3
Binary Numbers
The binary number system, i.e. in base two, uses only two digits to represent values: zero and one. This is clearly a limit example of representing values, because it uses the minimum number of digits. In practice it simplifies the conversion.
Following the logig of the previous examples, we analyse briefly the conversion of the binary number 11002: 0*20 + 0*21 + 1*22 + 1*23. Therefore, the same number could be represented as 12 according to the standard system. The other way round, to convert the number 12, one can proceed as follows: 12/2=6 with the remainder of zero; 6/2=3 with the remainder of zero; 3/2=1 with the remainder of one; 1/2=0 with the remainder of one. This way one gets back the binary number 11002.
32485 in base 10 --> | 32485/2 = 16242 with the remainder of 1
| 16242/2 = 8121 with the remainder of 0
| 8121/2 = 4060 with the remainder of 1
| 4060/2 = 2030 with the remainder of 0
| 2030/2 = 1015 with the remainder of 0
| 1015/2 = 507 with the remainder of 1
| 507/2 = 253 with the remainder of 1
| 253/2 = 126 with the remainder of 1
| 126/2 = 63 with the remainder of 0
| 63/2 = 31 with the remainder of 1
| 31/2 = 15 with the remainder of 1
| 15/2 = 7 with the remainder of 1
| 7/2 = 3 with the remainder of 1
| 3/2 = 1 with the remainder of 1
| 1/2 = 0 with the remainder of 1
|
`---------------------------> 111111011100101 in base 2
Figura 586.6. Computation of the value corresponding to a number expressed in base two.
The values listed in each box are the sequence of the powers of two: 20, 21, 22,... 2n.
If one wants to convert a binary number to a number in base 10, it is sufficient to enter the digits into the boxes, with right alignement, to multiply each digits by the value written above and then to sum up each result. For example:
To find the binary value correponding to a number in base 10, it is sufficient to subtract the largest possible value. Let's suppose that we want convert the number 123 in binary, one can subtract the values: 64, 32, 16, 8, 2 e 1:
586.3.1
Conversion among octal, hexadecimal and binary
The octal and hexadecimal number systems have the property of being easily convertable to binary and the other way round. Actually, an octal digit requires exactly three binary digits to represent it, whereas an hexadecimal digit requires four binary digits to represent it. For example, the octal number 1238 can be easily converted into the binary 0010100112; moreover the hexadecimal number 3C16 can be easily converted into 001111002.
Figura 586.10. Conversion between octal and binary numbers.
123 in base eight 001010011 in base two
||| \ /\ /\ /
||`------> 011 | | |
|| | | `----> 3
|`----> 010 | |
| | `------> 2
`--> 001 |
`--------> 1
= 001010011 in base two 123 in base eight
In practice, it is sufficient to convert each octal or hexadecimal digit in the corresponding binary number. Therefore, if one considers the 1238, one obtains 0012, 0102 and 0112, that can be linked as already shown. In the same way one can proceed for the 3C16, that converts into 00112 and 11002.
Figura 586.11. Conversion between hexadecimal and decimal numbers.
3C in base sixteen 00111100 in base two
|| \ /\ /
|| | |
|`-----> 1100 | |
| | `-----> C
`--> 0011 |
`--------> 3
= 00111100 in base two 3C in base sixteen
It is clearly simplified also the conversion from binary to octal and from binary to hexadecimal.
Figura 586.12. Summary of the conversion from binary to octal and from binary to hexadecimal.
111111011100101 in base two
\ /\ /\ /\ /\ /
7 7 3 4 5 in base eight
/ \/ \/ \/ \/ \
0111111011100101 in base two
\ /\ /\ /\ /
7 14 14 5
7 E E 5 in base sixteen
/ \/ \/ \/ \
0111111011100101 in base two
Table 586.1. Fast conversion from binary to octal and from binary to hexadecimal.