How to convert from decimal to octal. Converting numbers to binary, hexadecimal, decimal, octal number systems

Laboratory work №1

Topic: Number system. Converting integer decimal numbers to binary, octal, hexadecimal number systems. (1 hour), SRSP (1 hour).

Decimal number system

The name "decimal" is explained by the fact that this system is based on the base ten. This system uses ten digits to write numbers - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.

The decimal system is positional because the meaning of a digit in a decimal number depends on its position, or location, in the number.

The position allocated for the digit of a number is called digit.

For example, the entry 526 means that the number consists of 5 hundreds, 2 tens and 6 ones. The number 6 is in the units place. The number 2 is in the tens place and the number 5 is in the hundreds place.

Write this number as a sum:

526=5*10 2 +2*10 1 +6*10 0

in this entry the number 10 is the base of the number system. For each digit of a number, base 10 is raised to a power depending on the position of the digit and multiplied by that digit. The base power for units is zero, for tens it is one, for hundreds it is two, etc.

Negative exponents are used to write decimal fractions. For example, the number 555.55 in expanded form is written as follows:

555.55 10 = 5*10 2 + 5*10 1 + 5*10°+ 5*10- 1 +5*10- 2 .:

Converting whole decimal numbers to binary system Reckoning

When converting a decimal number to binary, you need to divide this number by 2. To convert a positive integer decimal number into the binary number system, you need to divide this number by 2. The resulting quotient is again divided by 2, etc. until the quotient is less than 2. As a result, write down the last quotient and all remainders, starting from the last, in one line.

Example. Convert the number 891 from the decimal system to the binary number system.

Solution:

1:2=0, 1 (most significant digit of binary number)

We write down in one line the last quotient and all remainders, starting from the last.

Answer: 891 10 =1101111011 2

Converting decimal fractions to binary number system

Converting decimal fractions to the binary number system involves finding the whole parts when multiplying by 2.

Example. Let's translate decimal 0.322 in binary number system.

To find the first decimal digit of a binary fraction, you need to multiply given number by 2 and highlight the whole part of the work.

Solution:

0,322 10 8,83 10

0.322*2=0.644 0 8:2=4 remainder 0

0.644*2=1.288 1 4:2=2 remainder 0

0.288*2=0.576 0 2:2=1 remainder 0

0.576*2=1.152 1 1:2=0 remainder 1

0,3222 10 =0.0101 2 0.83*2=1.66 whole part equals 1

0.66*2=1.32 the integer part is 1

0.32*2=0.64 the integer part is 0

0.64*2=1.28 the integer part is 1

Answer: 8.83=1000.1101

Converting decimal numbers to octal number system

To convert a number from the decimal system to octal, the same technique is used as when converting to the binary system.

The converted number is divided by 8 according to the rules of the decimal system, with the remainder being stored, which, of course, does not exceed 7. If the resulting quotient is greater than 7, it is also divided by 8, retaining the remainder.

Solution:

(most significant digit of a binary number).

Answer: 891 10 =1573 8

Converting numbers from one number system to another is important part machine arithmetic. Let's consider the basic rules of translation.

1. To convert a binary number to a decimal one, it is necessary to write it in the form of a polynomial, consisting of the products of the digits of the number and the corresponding power of 2, and calculate it according to the rules of decimal arithmetic:

When translating, it is convenient to use the table of powers of two:

Table 4. Powers of number 2

n (degree)

Example.

2. To convert an octal number to a decimal one, it is necessary to write it down as a polynomial consisting of the products of the digits of the number and the corresponding power of the number 8, and calculate it according to the rules of decimal arithmetic:

When translating, it is convenient to use the table of powers of eight:

Table 5. Powers of the number 8

n (degree)

Example. Convert number to decimal system Reckoning

3. To convert a hexadecimal number to a decimal one, it is necessary to write it in the form of a polynomial, consisting of the products of the digits of the number and the corresponding power of the number 16, and calculate it according to the rules of decimal arithmetic:

When translating, it is convenient to use blitz of powers of number 16:

Table 6. Powers of the number 16

n (degree)

Example. Convert the number to the decimal number system.

4. To convert a decimal number to the binary system, it must be successively divided by 2 until a remainder less than or equal to 1 remains. The number in the binary system is written as a sequence last result division and remainders from division in reverse order.

Example. Convert the number to the binary number system.

5. To convert a decimal number to the octal system, it must be sequentially divided by 8 until a remainder less than or equal to 7 remains. A number in the octal system is written as a sequence of digits of the last division result and the remainder of the division in reverse order.

Example. Convert the number to the octal number system.

6. To convert a decimal number to the hexadecimal system, it must be sequentially divided by 16 until there is a remainder less than or equal to 15. A number in the hexadecimal system is written as a sequence of digits of the last division result and the remainders from the division in reverse order.

Example. Convert the number to hexadecimal number system.

To represent numbers in a microprocessor it is used binary number system.
Moreover, any digital signal can have two stable states: " high level" And " low level" In the binary number system, two digits are used to represent any number, respectively: 0 and 1. Arbitrary number x=a n a n-1 ..a 1 a 0 ,a -1 a -2 …a -m will be written in binary number system as

x = a n ·2 n +a n-1 ·2 n-1 +…+a 1 ·2 1 +a 0 ·2 0 +a -1 ·2 -1 +a -2 ·2 -2 +…+a -m ·2 -m

Where a i— binary digits (0 or 1).

Octal number system

In the octal number system, the base digits are the numbers from 0 to 7. 8 low-order ones are combined into a high-order one.

Hexadecimal number system

In the hexadecimal number system, the base digits are the numbers from 0 to 15 inclusive. To designate base digits greater than 9 with one symbol, in addition to the Arabic numerals 0...9 in the hexadecimal number system, letters of the Latin alphabet are used:

10 10 = A 16 12 10 = C 16 14 10 = E 16
11 10 = B 16 13 10 = D 16 15 10 = F 16.

For example, the number 175 10 in hexadecimal number system will be written as AF 16. Really,

10·16 1 +15·16 0 =160+15=175

The table shows numbers from 0 to 16 in decimal, binary, octal and hexadecimal number systems.

Decimal Binary Octal Hexadecimal
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
16 10000 20 10

Binary-octal and binary-hexadecimal conversions

The binary number system is convenient for performing arithmetic operations using microprocessor hardware, but is inconvenient for human perception because it requires large quantity discharges. Therefore, in computer technology In addition to the binary number system, octal and hexadecimal number systems are widely used for a more compact representation of numbers.

The three digits of the octal number system implement all possible combinations of octal digits in the binary number system: from 0 (000) to 7 (111). To convert binary number to octal, you need to combine binary digits into groups of 3 digits (triads) in two directions, starting from the separator of the integer and fractional parts. If necessary, you need to add insignificant zeros to the left of the original number. If a number contains a fractional part, then to the right of it you can also add insignificant zeros until all triads are filled. Each triad is then replaced by an octal digit.

Example: Convert the number 1101110.01 2 to octal number system.

We combine binary digits into triads from right to left. We get

001 101 110,010 2 = 156,2 8 .

To convert a number from octal to binary, you need to write each octal digit in binary code:

156,2 8 = 001 101 110,010 2 .

The four digits of the hexadecimal number system implement all possible combinations of hexadecimal digits in the binary number system: from 0 (0000) to F(1111). To convert a binary number to hexadecimal, you need to combine the binary digits into groups of 4 digits (tetrads) in two directions, starting from the decimal separator. If necessary, you need to add insignificant zeros to the left of the original number. If the number contains a fractional part, then to the right of it you also need to add insignificant zeros until all notebooks are filled. Each tetrad is then replaced with a hexadecimal digit.

Example: Convert the number 1101110.11 2 to hexadecimal number system.

We combine binary digits into tetrads from right to left. We get

0110 1110.1100 2 = 6E,C 16 .

To convert a number from hexadecimal to binary, you need to write each hexadecimal digit in binary code.

1. Ordinal counting various systems Reckoning

IN modern life we use positioning systems notation, that is, systems in which the number denoted by a digit depends on the position of the digit in the notation of the number. Therefore, in the future we will talk only about them, omitting the term “positional”.

In order to learn how to convert numbers from one system to another, we will understand how sequential recording of numbers occurs using the example of the decimal system.

Since we have a decimal number system, we have 10 symbols (digits) to construct numbers. We start counting: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. The numbers are over. We increase the bit depth of the number and reset the low-order digit: 10. Then we increase the low-order digit again until all the digits are gone: 11, 12, 13, 14, 15, 16, 17, 18, 19. We increase the high-order digit by 1 and reset the low-order digit: 20. When we use all the digits for both digits (we get the number 99), we again increase the digit capacity of the number and reset the existing digits: 100. And so on.

Let's try to do the same in the 2nd, 3rd and 5th systems (we introduce the notation for the 2nd system, for the 3rd, etc.):

0 0 0 0
1 1 1 1
2 10 2 2
3 11 10 3
4 100 11 4
5 101 12 10
6 110 20 11
7 111 21 12
8 1000 22 13
9 1001 100 14
10 1010 101 20
11 1011 102 21
12 1100 110 22
13 1101 111 23
14 1110 112 24
15 1111 120 30

If the number system has a base greater than 10, then we will have to enter additional characters, it is customary to enter letters of the Latin alphabet. For example, for the 12-digit system, in addition to ten digits, we need two letters ( and ):

0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10
11
12 10
13 11
14 12
15 13

2. Conversion from the decimal number system to any other.

To convert a positive integer decimal number to a number system with a different base, you need to divide this number by the base. Divide the resulting quotient by the base again, and further until the quotient is less than the base. As a result, write down in one line the last quotient and all remainders, starting from the last.

Example 1. Let's convert the decimal number 46 to the binary number system.

Example 2. Let's convert the decimal number 672 to the octal number system.

Example 3. Let's convert the decimal number 934 to the hexadecimal number system.

3. Conversion from any number system to decimal.

In order to learn how to convert numbers from any other system to decimal, let's analyze the usual notation for a decimal number.
For example, the decimal number 325 is 5 units, 2 tens and 3 hundreds, i.e.

The situation is exactly the same in other number systems, only we will multiply not by 10, 100, etc., but by the powers of the base of the number system. For example, let's take the number 1201 in ternary system Reckoning Let's number the digits from right to left starting from zero and imagine our number as the sum of the products of a digit and three to the power of the digit of the number:

This is the decimal notation of our number, i.e.

Example 4. Let's convert to the decimal number system octal number 511.

Example 5. Let's convert the hexadecimal number 1151 to the decimal number system.

4. Conversion from the binary system to the system with the base “power of two” (4, 8, 16, etc.).

To convert a binary number to a number with the base “power of two”, it is necessary to divide the binary sequence into groups according to the number of digits equal to the power from right to left and replace each group with the corresponding digit new system Reckoning

For example, Let's convert the binary number 1100001111010110 to the octal system. To do this, we will divide it into groups of 3 characters starting from the right (since ), and then use the correspondence table and replace each group with a new number:

We learned how to build a correspondence table in step 1.

0 0
1 1
10 2
11 3
100 4
101 5
110 6
111 7

Those.

Example 6. Let's convert the binary number 1100001111010110 to hexadecimal.

0 0
1 1
10 2
11 3
100 4
101 5
110 6
111 7
1000 8
1001 9
1010 A
1011 B
1100 C
1101 D
1110 E
1111 F

5. Conversion from a system with the base “power of two” (4, 8, 16, etc.) to binary.

This translation is similar to the previous one, made in reverse side: We replace each digit with a group of binary digits from the lookup table.

Example 7. Let's convert the hexadecimal number C3A6 to the binary number system.

To do this, replace each digit of the number with a group of 4 digits (since ) from the correspondence table, supplementing the group with zeros at the beginning if necessary:



Converting numbers from binary to octal and hexadecimal and vice versa

Converting numbers between number systems whose bases are powers of 2 (q = 2 n) can be done using simpler algorithms. Such algorithms can be used to convert numbers between binary (q = 2 1), octal (q = 2 3) and hexadecimal (q = 2 4) number systems.

Converting numbers from binary to octal. To write binary numbers, two digits are used, that is, in each digit of the number, 2 writing options are possible. We solve the exponential equation:

2 = 2 i. Since 2 = 2 1, then i = 1 bit.

Each bit of a binary number contains 1 bit of information.

Eight digits are used to write octal numbers, that is, in each digit of the number there are 8 possible writing options. We solve the exponential equation:

8 = 2 i. Since 8 = 2 3, then i = 3 bits.

Each octal number contains 3 bits of information.

So, to convert an integer binary number to octal, you need to break it down into groups of three digits, from right to left, and then convert each group to an octal digit. If the last, left, group contains less than three digits, then it must be supplemented on the left with zeros.

Let's convert the binary number 101001 2 into octal in this way:

101 001 2 => 1 × 2 2 + 0 × 2 1 + 1 × 2 0 0 × 2 2 + 0 × 2 1 + 1 × 2 0 => 51 8 .

To simplify the translation, you can prepare in advance a table for converting binary triads (groups of 3 digits) into octal digits:

Binary triads 000 001 010 011 100 101 110 111
Octal digits 0 1 2 3 4 5 6 7

To convert a fractional binary number (proper fraction) into octal, you need to break it into triads from left to right and, if the last, right, group contains less than three digits, add zeros to the right. Next, you need to replace triads with octal numbers.

For example, let’s transform the fractional binary number A 2 = 0.110101 2 into the octal number system:

Binary triads 110 101
Octal digits 6 5

We get: A 8 = 0.65 8.

Converting numbers from binary to hexadecimal. To write hexadecimal numbers, sixteen digits are used, that is, in each digit of the number, 16 writing options are possible. We solve the exponential equation:

16 = 2 i. Since 16 = 2 4, then i = 4 bits.

Each digit of a hexadecimal number contains 4 bits of information.

Thus, to convert an integer binary number to hexadecimal, it must be divided into groups of four digits (tetrads), starting from the right, and, if the last left group contains less than four digits, pad it on the left with zeros. To convert a fractional binary number (proper fraction) into hexadecimal, you need to divide it into tetrads from left to right and, if the last right group contains less than four digits, then you need to pad it with zeros on the right.

Then you need to convert each group into a hexadecimal digit, using a previously compiled table of correspondence between binary tetrads and hexadecimal digits.

Let's convert the integer binary number A 2 = 101001 2 into hexadecimal:

We get: A 16 = 0.D4 16.

To convert any binary number to octal or hexadecimal system notation, it is necessary to carry out transformations using the algorithms discussed above separately for its integer and fractional parts.

Converting numbers from octal and hexadecimal number systems to binary. To convert numbers from octal and hexadecimal number systems to binary, you need to convert the digits of the number into groups binary digits. To convert from octal to binary, each digit of a number must be converted into a group of three binary digits (triad), and when converting a hexadecimal number, into a group of four digits (tetrad).

For example, let’s transform the fractional octal number A 8 = 0.47 8 into the binary number system:

As a result, we have: A 2 = 10101011 2

3tasks

1.16. Make a table of correspondence between binary tetrads and hexadecimal digits.

1.17. Convert the following integers to octal and hexadecimal number systems: 1111 2, 1010101 2.

1.18. Convert the following to octal and hexadecimal number systems: fractional numbers: 0,01111 2 , 0,10101011 2 .

1.19. Convert the following numbers to octal and hexadecimal number systems: 11.01 2, 110.101 2.

1.20. Convert the following numbers to the binary number system: 46.27 8, EF,12 16.

1.21. Compare numbers expressed in different number systems: 1101 2 and D 16; 0.11111 2 and 0.22 8; 35.63 8 and 16, C 16.