How to convert numbers to different number systems. Converting numbers to different number systems
Lesson objectives:
- repeat the material studied on the topic of the number system;
- learn to convert a number from the decimal system to any other positional number system and vice versa;
- master the principles of converting numbers from one system to another;
- develop logical thinking.
Lesson progress
At the beginning of the lesson, a brief review and checking of homework.
In what form is numerical information presented in computer memory?
What are number systems used for?
What types of number systems do you know? Give your own examples.
How do positional systems differ from non-positional systems?
The goal of our lesson is to learn how to convert a number from decimal system to any other positional number system and vice versa. But first we'll look at how you can
represent any non-negative integer:
IN positional systems the value of writing an integer is determined by the following rule: let a n a n-1 a n-2 …a 1 a 0 be a record of number A, and i are digits, then
where p is an integer greater than 1, which is called the base of the number system
In order for, for a given p, any non-negative integer could be written according to formula (1) and, moreover, in a unique way, the numerical values of the various digits must be different integers belonging to the segment from 0 to p-1.
1) Decimal system
numbers: 0,1,2,3,4,5,6,7,8,9
number 5735 = 5 10 3 +7 10 2 +3 10 1 +8 10 0
2) Ternary system
numbers: 0,1,2
number 201 3 = 2·3 2 +0·3 1 +1·3 0
Note: the subscript in a number indicates the base of the number system in which the number is written. For the decimal number system, the index need not be written.
Representation of negative and fractional numbers:
In all positional systems, the ‘–’ sign is used to write negative numbers, just like in the decimal system. A comma is used to separate the integer part of a number from the fractional part. The value of the entry a n a n-1 a n-2 …a 1 a 0 , a -1 a -2 …a m-2 a m-1 a m of the number A is determined by the formula, which is a generalization of formula (1):
75.6 = 7·10 1 +5·10 0 +6·10 –1
–2.314 5 = –(2 5 0 +3 5 –1 +1 5 –2 +4 5 –3)
Converting numbers from an arbitrary number system to decimal:
It should be understood that when translating a number from one number system to another, the quantitative value of the number does not change, but only the form of writing the number changes, just as when translating the name of a number, for example, from Russian into English.
Converting numbers from an arbitrary number system to decimal is performed by direct calculation using formula (1) for integers and formula (2) for fractions.
Converting numbers from the decimal number system to an arbitrary number system.
Converting a number from the decimal system to a system with base p means finding the coefficients in formula (2). Sometimes this is easy to do with a simple selection. For example, let's say you need to convert the number 23.5 to octal. It is easy to see that 23.5 = 16+7+0.5 = 2·8+7+4/8 = 2·8 1 +7·8 0 +4·8 –1 =27.48. It is clear that the answer is not always so obvious. In general, the method of converting the integer and fractional parts of a number separately is used.
To convert integers, the following algorithm is used (obtained based on formula (1)):
1. Find the quotient and remainder when dividing a number by p. The remainder will be the next digit ai (j=0,1,2...) of the number in the new number system.
2. If the quotient is equal to zero, then the translation of the number is completed, otherwise we apply point 1 to the quotient.
Note 1. The digits ai in the number notation are numbered from right to left.
Note 2. If p>10, then it is necessary to introduce notation for numbers with numerical values greater than or equal to 10.
Convert the number 165 to the septal number system.
165:7 = 23 (remainder 4) => a 0 = 4
23:7 = 3 (remainder 2) => a 1 = 2
3:7 = 0 (remainder 3) => a 2 = 3
Let's write down the result: a 2 a 1 a 0 , i.e. 3247.
Having checked using formula (1), we will make sure that the translation is correct:
3247=3·7 2 +2·7 1 +4·7 0 =3·49+2·7+4 = 147+14+4 = 165.
To convert fractional parts of numbers, an algorithm obtained based on formula (2) is used:
1. Multiply the fractional part of the number by p.
2. The integer part of the result will be the next digit am (m = –1, –2, –3 ...) of writing the number in the new number system. If the fractional part of the result is zero, then the translation of the number is completed, otherwise we apply step 1 to it.
Note 1. The digits a m in the number notation are arranged from left to right in ascending order of the absolute value of m.
Note 2. Usually the number of fractional digits in new entry numbers are limited in advance. This allows you to perform an approximate translation with a given accuracy. In the case of infinite fractions, such a restriction ensures the finiteness of the algorithm.
Convert the number 0.625 to the binary number system.
0.625 2 = 1.25 (integer part 1) => a -1 =1
0.25 2 = 0.5 (integer part 0) => a- 2 = 0
0.5 2 = 1.00 (integer part 1) => a- 3 = 1
So 0.62510 = 0.1012
Having checked using formula (2), we will make sure that the translation is correct:
0.1012=1·2 -1 +0·2- 2 +1·2 -3 =1/2+1/8 = 0.5+0.125 = 0.625.
Convert the number 0.165 to the quaternary number system, limiting it to four quaternary digits.
0.165 4 = 0.66 (integer part 0) => a -1 =0
0.66 4 = 2.64 (integer part 2) => a -2 = 2
0.64 4 = 2.56 (integer part 2) => a -3 = 2
0.56 4 = 2.24 (integer part 2) => a -4 = 2
So 0.16510" 0.02224
Let's do a back translation to make sure that the absolute error does not exceed 4–4:
0.02224 = 0·4 -1 +2·4 -2 +2·4 -3 +2·4 -4 = 2/16+2/64+2/256 = 1/8+1/32+1/ 128 = 21/128 = 0.1640625
|0,1640625–0,165| = 0,00094 < 4–4 = 0,00390625
Converting numbers from one arbitrary system to another
In this case, you must first convert the number to the decimal system, and then from the decimal system to the required one.
A special method is used to convert numbers for systems with multiple bases.
Let p and q be the bases of two number systems. We will call these systems number systems with multiple bases if p = qn or q = pn, where n is a natural number. So, for example, number systems with bases 2 and 8 are multiple base number systems.
Let p = qn and you need to convert a number from a number system with base q to a number system with base p. Let's divide the integer and fractional parts of the number into groups of n sequentially written digits to the left and right of the decimal point. If the number of digits in the integer part of a number is not a multiple of n, then you need to add the corresponding number of zeros to the left. If the number of digits in the fractional part of a number is not a multiple of n, then zeros are added to the right. Each such group of digits is a number in old system number will correspond to one digit of a number in the new number system.
Let's convert 1100001.111 2 to the quaternary number system.
By adding zeros and selecting pairs of numbers, we get 01100001.11102.
Now let’s translate each pair of digits separately, using the section Translating numbers from one arbitrary system to another.
So, 1100001.1112 = 01100001.11102 = 1201.324.
Let us now assume that we need to transfer from a system with a larger base q to a system with a smaller base p, i.e. q = pn. In this case, one digit of a number in the old number system corresponds to n digits of a number in the new number system.
Example: Let's check the previous translation of a number.
1201,324 = 1100001,11102=1100001,1112
In the hexadecimal system there are numbers with numerical values 10,11,12, 13,14,15. To designate them, use the first six letters of the Latin alphabet A, B, C, D, E, F.
Here is a table of numbers from 0 to 16, written in number systems with bases 10, 2, 8 and 16.
Number in decimal system | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
In octal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 20 |
In binary | 0 | 1 | 10 | 11 | 100 | 101 | 110 | 111 | 1000 | 1001 | 1010 | 1011 | 1100 | 1101 | 1110 | 1111 | 10000 |
In hexadecimal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | 10 |
You can also use lowercase digits to write hexadecimal digits. latin letters a-f.
Example: Let's convert the number 110101001010101010100.11 2 into hexadecimal number system.
Let's use the multiplicity of the bases of the number systems (16=2 4). Let's group the numbers by four, adding the required number of zeros to the left and right
000110101001010101010100,1100 2
and, checking the table, we get: 1A9554,C 16
Conclusion:
Which number system is best to write numbers in is a matter of convenience and tradition. From a technical point of view, it is convenient to use the binary system in a computer, since it uses only two digits 0 and 1 to record a number, which can be represented by two easily distinguishable states “no signal” and “there is a signal.”
On the contrary, it is inconvenient for a person to deal with binary numbers due to the fact that they are longer than decimal numbers and there are many repeating digits in them. Therefore, if necessary, work with machine representations of numbers, use octal or hexadecimal number systems. The bases of these systems are integer powers of two, and therefore numbers are easily converted from these systems to binary and vice versa.
Write down the homework assignment:
a) Write down the date of birth of all members of your family in different number systems.
b) Convert the numbers from binary to octal and hexadecimal, and then check the results by performing the reverse conversions:
a) 1001111110111.011 2;
When converting numbers from the decimal number system to any other, the whole and fractional parts are always translated separately (according to different rules).
Translation of the whole part
In order to convert a number from a decimal number system to any other, you need to perform integer division of the original number by the base of the number system to which you want to convert the number. In this case, the remainder of the division and the quotient are important. The quotient must be divided by the base until 0 remains. After this, all remainders must be written in reverse order - this will be the number in the new number system.
For example, converting the number 25 from the decimal number system to the binary system will look like this:
Writing out the remainders in reverse order, we get 25 10 =11001 2.
If you think about it, you can easily notice that when converting absolutely any number into the binary number system, the very last remainder (that is, the very first digit in the result) will always be equal to the very last quotient, which turned out to be less than the base of the number system in which we translate the number. Therefore, division is often stopped before the quotient becomes equal to zero - at the moment when the quotient becomes simply less than the base. For example:
Conversion from the decimal number system to any other number system is carried out according to exactly the same rules. Here is an example of converting 393 10 to hexadecimal number system:
Writing out the remainders in reverse order, we get 393 10 =189 16.
You need to understand that the remainders are obtained in the decimal number system. When dividing by 16, residues may appear not only from 0 to 9, but also residues from 10 to 15. Each remainder is always exactly one digit in the number system into which the translation is made.
For example, if, when converting to the hexadecimal number system, you received the following remainders (written out in the order they should be written in the number): 10, 3, 15, 7, then in the hexadecimal number system this sequence of remainders will correspond to the number A3F7 16 (some they mistakenly write the number as 103157 16 - it is clear that this is a completely different number, and that if you do this, it turns out that the numbers from A to F will not appear in any hexadecimal number).
Fractional translation
When translating a fractional part, unlike translating an entire part, you do not need to divide, but multiply by the base of the number system into which we are converting. In this case, whole parts are discarded each time, and fractional parts are multiplied again. By collecting the whole parts in the order in which they were obtained, we obtain the fractional part of the number in required system Reckoning.
One multiplication operation gives exactly one additional sign in the number system into which the translation is carried out.
In this case, there are two conditions for completing the process:
1) as a result of the next multiplication you received a zero in the fractional part. It is clear that no matter how much you multiply this zero, it will still remain zero. This means that the number has been converted from the decimal number system to the required one exactly.
2) not all numbers can be translated accurately. In this case, it is usually translated with some accuracy. In this case, they first determine how many decimal places will be needed - this is the number of times that the multiplication operation will need to be performed.
Here is an example of converting the number 0.39 10 to the binary number system. Accuracy - 8 digits (in in this case translation accuracy is chosen arbitrarily):
If we write out the whole parts in direct order, we get 0.39 10 =0.01100011 2 .
There is no need to write out the very first zero (crossed out in blue in the figure) - since it refers not to the fractional part, but to the whole part. Some people mistakenly write this zero after the decimal point when writing out the result.
This is what the conversion of the number 0.39 10 to hexadecimal number system will look like. Accuracy - 8 digits; in this case, the accuracy is again chosen arbitrarily:
If we write out the whole parts in direct order, we get 0.39 10 =0.63D700A3 16.
At the same time, you probably noticed that whole parts when multiplied are obtained in the decimal number system. These integer parts obtained when translating the fractional part of a number should be interpreted in exactly the same way as the remainders when translating the whole part of a number. That is, if, when converted to the hexadecimal number system, the integer parts turned out in this order: 3, 13, 7, 10, then the corresponding number will be equal to 0.3D7A 16 (and not 0.313710 16, as some sometimes mistakenly write).
Converting numbers with integer and fractional parts
To translate a number with an integer and a fractional part, you need to translate the integer part separately, and the fractional part separately, and then write these two parts together.
For example, 25.39 10 =11001.01100011 2 (translations of the integer and fractional parts - see above).
Converting small integers from decimal to binary in your head
Since when working with various number systems, especially when developing programs, the need to translate small integers very often arises, then, generally speaking, it makes sense to memorize the first 16 numbers (from 0 to 15).
But if you figure out how easy it is to mentally convert small integers from 0 to 15 from the decimal number system to binary, then you can simply calculate a significant part of the table in your head every time you need it. Do this operation many times, and at some point you yourself will not be able to understand whether you have already memorized the table or are still calculating.
So, to convert a small positive integer from 0 to 15 from decimal to binary, the first thing you need to understand is that each position in the binary number corresponds to a power of two. At the same time, powers of two for positions from 0 to 3 are very easy to remember - these are the numbers 1, 2, 4 and 8:
And the number 10 is 2 plus 8:
Well, the number 0 is a sin not to remember, since to get it you don’t need to add anything.
Note 1
If you want to convert a number from one number system to another, then it is more convenient to first convert it to the decimal number system, and only then convert it from the decimal number system to any other number system.
Rules for converting numbers from any number system to decimal
IN computer technology, using machine arithmetic, an important role is played by the conversion of numbers from one number system to another. Below we give the basic rules for such transformations (translations).
When converting a binary number to decimal, you need to represent binary number in the form of a polynomial, each element of which is represented as the product of a digit of a number and the corresponding power of the base number, in this case $2$, and then you need to calculate the polynomial according to the rules of decimal arithmetic:
$X_2=A_n \cdot 2^(n-1) + A_(n-1) \cdot 2^(n-2) + A_(n-2) \cdot 2^(n-3) + ... + A_2 \cdot 2^1 + A_1 \cdot 2^0$
Figure 1. Table 1
Example 1
Convert the number $11110101_2$ to the decimal number system.
Solution. Using the given table of $1$ powers of the base $2$, we represent the number as a polynomial:
$11110101_2 = 1 \cdot 27 + 1 \cdot 26 + 1 \cdot 25 + 1 \cdot 24 + 0 \cdot 23 + 1 \cdot 22 + 0 \cdot 21 + 1 \cdot 20 = 128 + 64 + 32 + 16 + 0 + 4 + 0 + 1 = 245_(10)$
To convert a number from the octal number system to the decimal number system, you need to represent it as a polynomial, each element of which is represented as the product of a digit of the number and the corresponding power of the base number, in this case $8$, and then you need to calculate the polynomial according to the rules of decimal arithmetic:
$X_8 = A_n \cdot 8^(n-1) + A_(n-1) \cdot 8^(n-2) + A_(n-2) \cdot 8^(n-3) + ... + A_2 \cdot 8^1 + A_1 \cdot 8^0$
Figure 2. Table 2
Example 2
Convert the number $75013_8$ to the decimal number system.
Solution. Using the given table of $2$ powers of the base $8$, we represent the number as a polynomial:
$75013_8 = 7\cdot 8^4 + 5 \cdot 8^3 + 0 \cdot 8^2 + 1 \cdot 8^1 + 3 \cdot 8^0 = 31243_(10)$
To convert a number from hexadecimal to decimal, you need to represent it as a polynomial, each element of which is represented as the product of a digit of the number and the corresponding power of the base number, in this case $16$, and then you need to calculate the polynomial according to the rules of decimal arithmetic:
$X_(16) = A_n \cdot 16^(n-1) + A_(n-1) \cdot 16^(n-2) + A_(n-2) \cdot 16^(n-3) + . .. + A_2 \cdot 16^1 + A_1 \cdot 16^0$
Figure 3. Table 3
Example 3
Convert the number $FFA2_(16)$ to the decimal number system.
Solution. Using the given table of $3$ powers of the base $8$, we represent the number as a polynomial:
$FFA2_(16) = 15 \cdot 16^3 + 15 \cdot 16^2 + 10 \cdot 16^1 + 2 \cdot 16^0 =61440 + 3840 + 160 + 2 = 65442_(10)$
Rules for converting numbers from the decimal number system to another
- To convert a number from the decimal number system to the binary system, it must be sequentially divided by $2$ until there is a remainder less than or equal to $1$. Number in binary system represent it as a sequence of the last division result and the remainders from the division in reverse order.
Example 4
Convert the number $22_(10)$ to the binary number system.
Solution:
Figure 4.
$22_{10} = 10110_2$
- To convert a number from the decimal number system to octal, it must be sequentially divided by $8$ until there is a remainder less than or equal to $7$. A number in the octal number system is represented as a sequence of digits of the last division result and the remainders from the division in reverse order.
Example 5
Convert the number $571_(10)$ to the octal number system.
Solution:
Figure 5.
$571_{10} = 1073_8$
- To convert a number from the decimal number system to the hexadecimal system, it must be successively divided by $16$ until there is a remainder less than or equal to $15$. A number in the hexadecimal system is represented as a sequence of digits of the last division result and the remainder of the division in reverse order.
Example 6
Convert the number $7467_(10)$ to hexadecimal number system.
Solution:
Figure 6.
$7467_(10) = 1D2B_(16)$
In order to convert a proper fraction from a decimal number system to a non-decimal number system, it is necessary to sequentially multiply the fractional part of the number being converted by the base of the system to which it needs to be converted. Fractions in the new system will be represented as whole parts of products, starting with the first.
For example: $0.3125_((10))$ in octal number system will look like $0.24_((8))$.
In this case, you may encounter a problem when a finite decimal fraction can correspond to an infinite (periodic) fraction in the non-decimal number system. In this case, the number of digits in the fraction represented in the new system will depend on the required accuracy. It should also be noted that integers remain integers, and proper fractions remain fractions in any number system.
Rules for converting numbers from a binary number system to another
- To convert a number from the binary number system to octal, it must be divided into triads (triples of digits), starting with the least significant digit, if necessary, adding zeros to the leading triad, then replace each triad with the corresponding octal digit according to Table 4.
Figure 7. Table 4
Example 7
Convert the number $1001011_2$ to the octal number system.
Solution. Using Table 4, we convert the number from the binary number system to octal:
$001 001 011_2 = 113_8$
- To convert a number from the binary number system to hexadecimal, it should be divided into tetrads (four digits), starting with the least significant digit, if necessary, adding zeros to the most significant tetrad, then replace each tetrad with the corresponding octal digit according to Table 4.
You can enter both whole numbers, for example 34, and fractional numbers, for example, 637.333. For fractional numbers, the translation accuracy after the decimal point is indicated.
The following are also used with this calculator:
Ways to represent numbers
Binary (binary) numbers - each digit means the value of one bit (0 or 1), the most significant bit is always written on the left, the letter “b” is placed after the number. For ease of perception, notebooks can be separated by spaces. For example, 1010 0101b.Hexadecimal (hexadecimal) numbers - each tetrad is represented by one symbol 0...9, A, B, ..., F. This representation can be designated in different ways, here only the symbol “h” is used after the last hexadecimal digit. For example, A5h. In program texts, the same number can be designated as either 0xA5 or 0A5h, depending on the syntax of the programming language. A leading zero (0) is added to the left of the most significant hexadecimal digit represented by the letter to distinguish between numbers and symbolic names.
Decimal (decimal) numbers – each byte (word, double word) is represented regular number, and the decimal representation sign (the letter “d”) is usually omitted. The byte in the previous examples has a decimal value of 165. Unlike binary and hexadecimal notation, decimal is difficult to mentally determine the value of each bit, which is sometimes necessary.
Octal (octal) numbers - each triple of bits (division starts from the least significant) is written as a number 0–7, with an “o” at the end. The same number would be written as 245o. The octal system is inconvenient because the byte cannot be divided equally.
Algorithm for converting numbers from one number system to another
Converting whole decimal numbers to any other number system is carried out by dividing the number by the base new system numbering until the remainder remains a number smaller than the base of the new number system. The new number is written as division remainders, starting from the last one.Converting a regular decimal fraction to another PSS is carried out by multiplying only the fractional part of the number by the base of the new number system until all zeros remain in the fractional part or until the specified translation accuracy is achieved. As a result of each multiplication operation, one digit of a new number is formed, starting with the highest one.
Translation of improper fractions is carried out according to rules 1 and 2. The integer and fractional parts are written together, separated by a comma.
Example No. 1.
Conversion from 2 to 8 to 16 number system.
These systems are multiples of two, therefore the translation is carried out using a correspondence table (see below).
To convert a number from the binary number system to the octal (hexadecimal) number system, it is necessary to divide the binary number from the decimal point to the right and left into groups of three (four for hexadecimal) digits, supplementing the outer groups with zeros if necessary. Each group is replaced by the corresponding octal or hexadecimal digit.
Example No. 2. 1010111010.1011 = 1.010.111.010.101.1 = 1272.51 8
here 001=1; 010=2; 111=7; 010=2; 101=5; 001=1
When converting to the hexadecimal system, you must divide the number into parts of four digits, following the same rules.
Example No. 3. 1010111010,1011 = 10.1011.1010,1011 = 2B12,13 HEX
here 0010=2; 1011=B; 1010=12; 1011=13
Converting numbers from 2, 8 and 16 to the decimal system is done by breaking the number into individual ones and multiplying it by the base of the system (from which the number is translated) raised to the power corresponding to it serial number in the translated number. In this case, the numbers are numbered to the left of the decimal point (the first number is numbered 0) with increasing, and to the right with decreasing (i.e., with a negative sign). The results obtained are added up.
Example No. 4.
An example of conversion from binary to decimal number system.
1010010.101 2 = 1·2 6 +0·2 5 +1·2 4 +0·2 3 +0·2 2 +1·2 1 +0·2 0 + 1·2 -1 +0·2 - 2 +1 2 -3 =
= 64+0+16+0+0+2+0+0.5+0+0.125 = 82.625 10 An example of conversion from octal to decimal number system. 108.5 8 = 1*·8 2 +0·8 1 +8·8 0 + 5·8 -1 = 64+0+8+0.625 = 72.625 10 An example of conversion from hexadecimal to decimal number system. 108.5 16 = 1·16 2 +0·16 1 +8·16 0 + 5·16 -1 = 256+0+8+0.3125 = 264.3125 10
Once again we repeat the algorithm for converting numbers from one number system to another PSS
- From the decimal number system:
- divide the number by the base of the number system being translated;
- find the remainder when dividing an integer part of a number;
- write down all remainders from division in reverse order;
- From the binary number system
- To convert to the decimal number system, it is necessary to find the sum of the products of base 2 by the corresponding degree of digit;
- To convert a number to octal, you need to break the number into triads.
For example, 1000110 = 1,000 110 = 106 8 - To convert a number from binary to hexadecimal, you need to divide the number into groups of 4 digits.
For example, 1000110 = 100 0110 = 46 16
Number system correspondence table:
Binary SS | Hexadecimal SS |
0000 | 0 |
0001 | 1 |
0010 | 2 |
0011 | 3 |
0100 | 4 |
0101 | 5 |
0110 | 6 |
0111 | 7 |
1000 | 8 |
1001 | 9 |
1010 | A |
1011 | B |
1100 | C |
1101 | D |
1110 | E |
1111 | F |
Table for conversion to octal number system
The result has already been received!
Number systems
There are positional and non-positional number systems. The Arabic number system, which we use in everyday life, is positional, but the Roman number system is not. In positional number systems, the position of a number uniquely determines the magnitude of the number. Let's consider this using the example of the number 6372 in the decimal number system. Let's number this number from right to left starting from zero:
Then the number 6372 can be represented as follows:
6372=6000+300+70+2 =6·10 3 +3·10 2 +7·10 1 +2·10 0 .
The number 10 determines the number system (in this case it is 10). The values of the position of a given number are taken as powers.
Consider the real decimal number 1287.923. Let's number it starting from zero, positioning the number from the decimal point to the left and right:
Then the number 1287.923 can be represented as:
1287.923 =1000+200+80 +7+0.9+0.02+0.003 = 1·10 3 +2·10 2 +8·10 1 +7·10 0 +9·10 -1 +2·10 -2 +3· 10 -3.
In general, the formula can be represented as follows:
C n s n +C n-1 · s n-1 +...+C 1 · s 1 +C 0 ·s 0 +D -1 ·s -1 +D -2 ·s -2 +...+D -k ·s -k
where C n is an integer in position n, D -k - fractional number in position (-k), s- number system.
A few words about number systems. A number in the decimal number system consists of many digits (0,1,2,3,4,5,6,7,8,9), in the octal number system it consists of many digits (0,1, 2,3,4,5,6,7), in the binary number system - from a set of digits (0,1), in the hexadecimal number system - from a set of digits (0,1,2,3,4,5,6, 7,8,9,A,B,C,D,E,F), where A,B,C,D,E,F correspond to the numbers 10,11,12,13,14,15. In the table Tab.1 numbers are presented in different number systems.
Table 1 | |||
---|---|---|---|
Notation | |||
10 | 2 | 8 | 16 |
0 | 0 | 0 | 0 |
1 | 1 | 1 | 1 |
2 | 10 | 2 | 2 |
3 | 11 | 3 | 3 |
4 | 100 | 4 | 4 |
5 | 101 | 5 | 5 |
6 | 110 | 6 | 6 |
7 | 111 | 7 | 7 |
8 | 1000 | 10 | 8 |
9 | 1001 | 11 | 9 |
10 | 1010 | 12 | A |
11 | 1011 | 13 | B |
12 | 1100 | 14 | C |
13 | 1101 | 15 | D |
14 | 1110 | 16 | E | 15 | 1111 | 17 | F |
Converting numbers from one number system to another
To convert numbers from one number system to another, the easiest way is to first convert the number to the decimal number system, and then convert from the decimal number system to the required number system.
Converting numbers from any number system to the decimal number system
Using formula (1), you can convert numbers from any number system to the decimal number system.
Example 1. Convert the number 1011101.001 from binary number system (SS) to decimal SS. Solution:
1 ·2 6 +0 ·2 5 + 1 ·2 4 + 1 ·2 3 + 1 ·2 2 + 0 ·2 1 + 1 ·2 0 + 0 ·2 -1 + 0 ·2 -2 + 1 ·2 -3 =64+16+8+4+1+1/8=93.125
Example2. Convert the number 1011101.001 from octal system notation (SS) to decimal SS. Solution:
Example 3 . Convert the number AB572.CDF from hexadecimal number system to decimal SS. Solution:
Here A-replaced by 10, B- at 11, C- at 12, F- by 15.
Converting numbers from the decimal number system to another number system
To convert numbers from the decimal number system to another number system, you need to convert the integer part of the number and the fractional part of the number separately.
The integer part of a number is converted from decimal SS to another number system by sequentially dividing the integer part of the number by the base of the number system (for binary SS - by 2, for 8-ary SS - by 8, for 16-ary SS - by 16, etc. ) until a whole residue is obtained, less than the base CC.
Example 4 . Let's convert the number 159 from decimal SS to binary SS:
159 | 2 | ||||||
158 | 79 | 2 | |||||
1 | 78 | 39 | 2 | ||||
1 | 38 | 19 | 2 | ||||
1 | 18 | 9 | 2 | ||||
1 | 8 | 4 | 2 | ||||
1 | 4 | 2 | 2 | ||||
0 | 2 | 1 | |||||
0 |
As can be seen from Fig. 1, the number 159 when divided by 2 gives the quotient 79 and remainder 1. Further, the number 79 when divided by 2 gives the quotient 39 and remainder 1, etc. As a result, constructing a number from division remainders (from right to left), we obtain a number in binary SS: 10011111 . Therefore we can write:
159 10 =10011111 2 .
Example 5 . Let's convert the number 615 from decimal SS to octal SS.
615 | 8 | ||
608 | 76 | 8 | |
7 | 72 | 9 | 8 |
4 | 8 | 1 | |
1 |
When converting a number from decimal SS to octal SS, you need to sequentially divide the number by 8 until you get an integer remainder less than 8. As a result, constructing a number from division remainders (from right to left) we get a number in octal SS: 1147 (See Fig. 2). Therefore we can write:
615 10 =1147 8 .
Example 6 . Let's convert the number 19673 from the decimal number system to hexadecimal SS.
19673 | 16 | ||
19664 | 1229 | 16 | |
9 | 1216 | 76 | 16 |
13 | 64 | 4 | |
12 |
As can be seen from Figure 3, by successively dividing the number 19673 by 16, the remainders are 4, 12, 13, 9. In the hexadecimal number system, the number 12 corresponds to C, the number 13 - D. Therefore, our hexadecimal number- this is 4CD9.
To convert proper decimal fractions ( real number with a zero integer part) into a number system with base s, it is necessary to sequentially multiply this number by s until the fractional part is pure zero, or we obtain the required number of digits. If the multiplication results in a number with an integer part other than zero, then this integer part is not taken into account (they are sequentially included in the result).
Let's look at the above with examples.
Example 7 . Let's convert the number 0.214 from the decimal number system to binary SS.
0.214 | ||
x | 2 | |
0 | 0.428 | |
x | 2 | |
0 | 0.856 | |
x | 2 | |
1 | 0.712 | |
x | 2 | |
1 | 0.424 | |
x | 2 | |
0 | 0.848 | |
x | 2 | |
1 | 0.696 | |
x | 2 | |
1 | 0.392 |
As can be seen from Fig. 4, the number 0.214 is sequentially multiplied by 2. If the result of multiplication is a number with an integer part other than zero, then the integer part is written separately (to the left of the number), and the number is written with a zero integer part. If the multiplication results in a number with a zero integer part, then a zero is written to the left of it. The multiplication process continues until the fractional part reaches a pure zero or we obtain the required number of digits. Writing bold numbers (Fig. 4) from top to bottom we get the required number in the binary number system: 0. 0011011 .
Therefore we can write:
0.214 10 =0.0011011 2 .
Example 8 . Let's convert the number 0.125 from the decimal number system to binary SS.
0.125 | ||
x | 2 | |
0 | 0.25 | |
x | 2 | |
0 | 0.5 | |
x | 2 | |
1 | 0.0 |
To convert the number 0.125 from decimal SS to binary, this number is sequentially multiplied by 2. In the third stage, the result is 0. Consequently, the following result is obtained:
0.125 10 =0.001 2 .
Example 9 . Let's convert the number 0.214 from the decimal number system to hexadecimal SS.
0.214 | ||
x | 16 | |
3 | 0.424 | |
x | 16 | |
6 | 0.784 | |
x | 16 | |
12 | 0.544 | |
x | 16 | |
8 | 0.704 | |
x | 16 | |
11 | 0.264 | |
x | 16 | |
4 | 0.224 |
Following examples 4 and 5, we get the numbers 3, 6, 12, 8, 11, 4. But in hexadecimal SS, the numbers 12 and 11 correspond to the numbers C and B. Therefore, we have:
0.214 10 =0.36C8B4 16 .
Example 10 . Let's convert the number 0.512 from the decimal number system to octal SS.
0.512 | ||
x | 8 | |
4 | 0.096 | |
x | 8 | |
0 | 0.768 | |
x | 8 | |
6 | 0.144 | |
x | 8 | |
1 | 0.152 | |
x | 8 | |
1 | 0.216 | |
x | 8 | |
1 | 0.728 |
Received:
0.512 10 =0.406111 8 .
Example 11 . Let's convert the number 159.125 from the decimal number system to binary SS. To do this, we translate separately the integer part of the number (Example 4) and the fractional part of the number (Example 8). Further combining these results we get:
159.125 10 =10011111.001 2 .
Example 12 . Let's convert the number 19673.214 from the decimal number system to hexadecimal SS. To do this, we translate separately the integer part of the number (Example 6) and the fractional part of the number (Example 9). Further, combining these results we obtain.