Converting numbers to other number systems. Converting numbers from decimal to binary and back

To quickly convert numbers from the decimal number system to the binary system, you need to have a good knowledge of the numbers “2 to the power”. For example, 2 10 =1024, etc. This will allow you to solve some translation examples literally in seconds. One of these tasks is Problem A1 from the USE demo 2012. You can, of course, take a long and tedious time to divide a number by “2”. But it’s better to decide differently, saving valuable time on the exam.

The method is very simple. Its gist is this: If the number that needs to be converted from the decimal system is equal to the number "2 to the power", then this number in the binary system contains a number of zeros equal to the power. We add a “1” in front of these zeros.

  • Let's convert the number 2 from the decimal system. 2=2 1 . Therefore, in the binary system, a number contains 1 zero. We put “1” in front and get 10 2.
  • Let's convert 4 from the decimal system. 4=2 2 . Therefore, in the binary system, a number contains 2 zeros. We put “1” in front and get 100 2.
  • Let's convert 8 from the decimal system. 8=2 3 . Therefore, in the binary system, a number contains 3 zeros. We put “1” in front and get 1000 2.


Similarly for other numbers "2 to the power".

If the number that needs to be converted is less than the number “2 to the power” by 1, then in the binary system this number consists only of units, the number of which is equal to the power.

  • Let's convert 3 from the decimal system. 3=2 2 -1. Therefore, in the binary system, a number contains 2 ones. We get 11 2.
  • Let's convert 7 from the decimal system. 7=2 3 -1. Therefore, in the binary system, a number contains 3 ones. We get 111 2.

In the figure, the squares indicate the binary representation of the number, and the pink color on the left indicates the decimal representation.


The translation is similar for other numbers “2 to the power-1”.

It is clear that the translation of numbers from 0 to 8 can be done quickly or by division, or simply know by heart their representation in the binary system. I gave these examples so that you understand the principle of this method and use it to translate more “impressive numbers”, for example, to translate the numbers 127,128, 255, 256, 511, 512, etc.

You can come across such problems when you need to convert a number that is not equal to the number “2 to the power”, but close to it. It may be greater or less than 2 to the power. The difference between the translated number and the number "2 to the power" should be small. For example, up to 3. The representation of numbers from 0 to 3 in the binary system just needs to be known without translation.

If the number is greater than , then solve like this:

First we convert the number “2 to the power” into the binary system. And then we add to it the difference between the number “2 to the power” and the number being translated.

For example, let's convert 19 from the decimal system. It is greater than the number "2 to the power" by 3.

16=2 4 . 16 10 =10000 2 .

3 10 =11 2 .

19 10 =10000 2 +11 2 =10011 2 .

If the number is less than the number "2 to the power", then it is more convenient to use the number "2 to the power-1". We solve it like this:

First we convert the number “2 to the power-1” into the binary system. And then we subtract from it the difference between the number “2 to the power of 1” and the number being translated.

For example, let's convert 29 from the decimal system. It is greater than the number “2 to the power-1” by 2. 29=31-2.

31 10 =11111 2 .

2 10 =10 2 .

29 10 =11111 2 -10 2 =11101 2

If the difference between the number being translated and the number "2 to the power" is more than three, then you can break the number into its components, convert each part into the binary system and add.

For example, convert the number 528 from the decimal system. 528=512+16. We translate 512 and 16 separately.
512=2 9 . 512 10 =1000000000 2 .
16=2 4 . 16 10 =10000 2 .
Now let's add it in a column:

In this article I will tell you the basics of computer technology - this is a binary system. This is the lowest level, these are the numbers by which the computer works. And you will learn how to transfer from one system

Table 1 - Representation of numbers in various systems
calculus (beginning)

Number systems

Decimal

Binary

Octal

Hexadecimal

BCD

To convert from decimal to binary, you have two options.

1) For example, the number 37 needs to be converted from the decimal system to the binary system, then you need to divide it by two, and then check the remainder of the division. If the remainder is odd, then we write one at the bottom and the next division cycle goes through an even number; if the remainder of the division is even, then we write zero. At the end you must get 1. And now we convert the resulting result into binary, and the number goes from right to left.

Step by step: 37 is an odd number, which means 1 , then 36/2 = 18. The number is even, which means 0. 18/2 = 9 is an odd number, which means 1 , then 8/2 = 4. The number is even, read 0. 4/2 = 2, an even number means 0, 2/2 = 1.

So we got the number. Don't forget to count from right to left: 100101 - now we have a number in the binary system. In general, this is written as a division in a column, as you see in the figure below:

2) But there is a second way. I like him better. Transfer from one system to another is as follows:

where ai is the i-th digit of the number;
k - the number of digits in the fractional part of the number;
m - the number of digits in the integer part of the number;
N is the base of the number system.

The base of the number system N shows how many times the “weight” of the i-th digit is greater than the “weight” (i-1) of the digit. The integer part of the number is separated from the fractional part by a dot (comma).

The integer part of the number AN1, with the base N1, is converted to the number system with the base N2 by sequentially dividing the integer part of the number AN1 by the base N2 written as a number with the base N1, until a remainder is obtained. The resulting part is again divided by the base N2, and this process must repeat until the particle becomes smaller than the divisor. The resulting remainders from division and the last part are written in the reverse order obtained during division. The generated number will be an integer with base N2.

The fractional part of the number AN1, with base N1, is converted into a number system with base N2 by sequentially multiplying the fractional part of the number AN1 by base N2, written as a number with base N1. With each multiplication, the integer part of the product is taken in the form of the next digit of the corresponding digit, and the fractional part of the remaining is taken as a new multiplication. The number of multiplications determines the digit capacity of the resulting result, representing the fractional part of the number AN1 in the N2 number system. The fractional part of a number is often represented inaccurately when translated.

Let's do this with an example:

Convert from decimal to binary

37 in decimal must be converted to binary. Let's work with degrees:

2 0 = 1
2 1 = 2
2 2 = 4
2 3 = 8
2 4 = 16
2 5 = 32
2 6 = 64
2 7 = 128
2 8 = 256
2 9 = 512
2 10 = 1024 and so on... ad infinitum

This means: 37 - 32 = 5. 5 - 4 = 1. The answer is as follows in binary: 100101.

Let's convert the number 658 from decimal to binary:

658-512=146
146-128=18
18-16=2. In the binary system the number will look like: 1010010010.

Converting from decimal to octal

If you need to convert from decimal to octal, you must first convert to binary, and then convert from binary to octal. That is, it’s easier this way, although you can translate it right away. Using an algorithm similar to the one for converting to binary, see above.

Convert from decimal to hexadecimal

If you need to convert from decimal to hexadecimal, you must first convert to binary and then convert from binary to hexadecimal. That is, it’s easier this way, although you can translate it right away. Using an algorithm similar to the one for converting to binary, see above.

Converting from binary to octal

To convert a number from binary to octal, you need to split the binary into three numbers.

For example, the resulting number 1010010010 is divided into three numbers, and the division goes from right to left: 1,010,010,010 = 1222. See the table at the very beginning.

Converting from binary to hexadecimal

To convert a number from binary to hexadecimal, you need to divide it into tetrads (four each)

10 1001 0010 = 292

Here are a few examples for you to look through:

Conversion is from binary to octal, then to hexadecimal, and then from binary to decimal

(2) = 11101110
(8) = 11 101 110 = 276
(16) = 1110 1110 = EE
(10) = 1*128+ 1*64+ 1*32+ 0 +1*8 + 1*4 + 1*2+ 0= 238
3) (8) = 657

Conversion is carried out from hexadecimal to binary, then to octal, and then from binary to decimal

(16) = 6E8
(2) = 110 1110 1000
(8) = 11 011 101 000 = 2250
(10) = 1*1024+1*512+ 0 +1*128+ 1*64+ 1*32+ 8 = 1768

In one of our materials we looked at the definition. It has the shortest alphabet. Only two digits: 0 and 1. Examples of alphabets of positional number systems are given in the table.

Positional number systems

System name

Base

Alphabet

Binary

Trinity

Quaternary

Fivefold

Octal

Decimal

0,1,2,3,4,5,6,7,8,9

duodecimal

0,1,2,3,4,5,6,7,8,9,A,B

Hexadecimal

0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F

Thirty-six

0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G, H,I,J,K,L,M,N,O, P,R,S,T,U,V,X,Y,Z


To convert a small number from decimal to binary and vice versa, it is better to use the following table.

Table for converting decimal numbers from 0 to 20 to the binary number system.

decimal

number

binary number

decimal

number

binary number


However, the table will turn out to be huge if you write all the numbers there. Finding the right number among them will be more difficult. It is much easier to remember several algorithms for converting numbers from one positional number system to another.


How to convert from one number system to another? In computer science, there are several simple ways to convert decimal numbers to binary numbers. Let's look at two of them.

Method number 1.

Let's say you need to convert a number 637 decimal system to binary system.


This is done as follows: the maximum power of two is found so that two in this power is less than or equal to the original number.


In our case it is 9, because 2 9 =512 , A 2 10 =1024 , which is greater than our starting number. Thus, we received the number of digits of the result. It is equal to 9+1=10. This means that the result will look like 1ххххххххх, where x can be replaced by 1 or 0.


Let's find the second digit of the result. Let's raise two to the power of 9 and subtract from the original number: 637-2 9 =125. Then compare with the number 2 8 =256 . Since 125 is less than 256, the ninth digit will be 0, i.e. the result will already look like 10хххххххх.


2 7 =128 > 125 , which means the eighth digit will also be zero.


2 6 =64 , then the seventh digit is equal to 1. 125-64=61 Thus, we have received four senior digits and the number will take the form 10011ххххх.


2 5 =32 and we see that 32< 61, значит шестой разряд равен 1 (результат 100111хххх), остаток 61-32=29.


2 4 =16 < 29 - fifth digit 1 => 1001111xxx. Remainder 29-16=13.


2 3 =8 < 13 => 10011111хх. 13-8=5


2 2 =4 < 5 => 10011111хх, remainder 5-4=1.


2 1 =2 > 1 => 100111110x, remainder 2-1=1.


2 0 =1 => 1001111101.


This will be the final result.

Method No. 2.

The rule for converting integer decimal numbers to the binary number system states:

  1. Let's divide a n−1 a n−2 ...a 1 a 0 =a n−1⋅2 n−1 +a n−2⋅2 n−2 +...+a 0⋅2 0 by 2.
  2. The quotient will be equal to an−1⋅2n−2+...+a1, and the remainder will be equal
  3. Let us again divide the resulting quotient by 2, the remainder of the division will be equal to a1.
  4. If we continue this division process, then at the nth step we get a set of numbers: a 0 ,a 1 ,a 2 ,...,a n−1, which are included in the binary representation of the original number and coincide with the remainders when it is sequentially divided by 2.
  5. Thus, to convert an integer decimal number to the binary number system, you need to sequentially divide the given number and the resulting integer quotients by 2 until we get a quotient that is equal to zero.

The original number in the binary number system is compiled by sequentially recording the resulting remainders. We start recording it with the last one found.


Let's convert the decimal number 11 into the binary number system. The sequence of actions discussed above (translation algorithm) can be depicted as follows:


Got 11 10 =1011 2 .

Example:

If the decimal number is large enough, then the following way of writing the algorithm discussed above is more convenient:



363 10 =101101011 2



We encounter the binary number system when studying computer disciplines. After all, it is on the basis of this system that the processor and some types of encryption are built. There are special algorithms for writing a decimal number in the binary system and vice versa. If you know the principle of building a system, it will not be difficult to operate in it.

The principle of constructing a system of zeros and ones

The binary number system is built using two digits: zero and one. Why these particular numbers? This is due to the principle of constructing the signals that are used in the processor. At its lowest level, the signal takes only two values: false and true. Therefore, it was customary to denote the absence of a signal, “false,” by zero, and its presence, “true,” by one. This combination is easy to implement technically. Numbers in the binary system are formed in the same way as in the decimal system. When a digit reaches its upper limit, it is reset to zero and a new digit is added. This principle is used to move through a ten in the decimal system. Thus, numbers are made up of combinations of zeros and ones, and this combination is called the “binary number system”.

Recording a number in the system

In decimal

In binary

In decimal

In binary

How to write a binary number as a decimal number?

There are online services that convert numbers into binary and vice versa, but it’s better to be able to do it yourself. When translated, the binary system is denoted by the subscript 2, for example, 101 2. Each number in any system can be represented as a sum of numbers, for example: 1428 = 1000 + 400 + 20 + 8 - in the decimal system. The number is also represented in binary. Let's take an arbitrary number 101 and consider it. It has 3 digits, so we arrange the number in order in this way: 101 2 =1×2 2 +0×2 1 +1×2 0 =4+1=5 10, where the index 10 denotes the decimal system.

How to write a prime number in binary?

It is very easy to convert to the binary number system by dividing the number by two. It is necessary to divide until it is possible to complete it completely. For example, take the number 871. We begin to divide, making sure to write down the remainder:

871:2=435 (remainder 1)

435:2=217 (remainder 1)

217:2=108 (remainder 1)

The answer is written according to the resulting remainders in the direction from end to beginning: 871 10 =101100111 2. You can check the correctness of the calculations using the reverse translation described earlier.

Why do you need to know translation rules?

The binary number system is used in most disciplines related to microprocessor electronics, coding, data transmission and encryption, and in various areas of programming. Knowledge of the basics of translation from any system to binary will help the programmer develop various microcircuits and control the operation of the processor and other similar systems programmatically. The binary number system is also necessary for implementing methods for transmitting data packets over encrypted channels and creating client-server software projects based on them. In a school computer science course, the basics of converting to the binary system and vice versa are the basic material for studying programming in the future and creating simple programs.

Rule. To convert a number from one number system to another, you need to divide the original number by the base of the new number system. Divide the resulting quotient again by the base of the new number system, and continue the division until then. until the quotient is less than the base of the new number system. The resulting division remainders, starting from the last, are written in reverse order. This will be the recording of the number in the new number system.

Example. Convert the number 135 from decimal SS to 2-ary, octal and hexadecimal number systems.

1) 2) 3)

Task 2.

Convert the following numbers to binary, octal and hexadecimal SS: 1275,973, 172

Reverse conversion of numbers from any SS to decimal.

1) To convert a number from any SS to the original SS (reverse translation), you need to multiply each digit of this number by the base of the original SS. starting from the zero digit from right to left, and add the products. If you are converting a decimal fraction, you should apply the rule for writing the integer and fractional parts of the number.

2) Reverse translation of numbers is carried out according to the formula:

where A is a given number,

g – SS base of a given number (=2 for 2-ary SS, for other SS - similar),

m – the number of digits in the integer part of the number.

n – number of digits in the fractional part of the number,

a – the value of the digits of a given number (the fractional part of the number is highlighted in blue).

110110 2 = 1*2 5 +1*2 4 +0*2 3 +1*2 2 +1*2 1 +0*2 0 =54 10

66 8 =6*8 1 +6*8 0 =48+6=54 10 9A 16 =9*16 1 +10*16 0 =144+10=154 10

13.4 8 =1*8 1 +3*8 0 +4*8 -1 =8+3+0.5=11.5 10 (this number is a decimal fraction)

Task 3.

Convert the following numbers to decimal SS:

101,11 2 =5,75 10 1011001 2 1011,101 2

125,7 8 =86 10 1253 8 175,132 8

A19BA 16 =2585726… 10 16A3 16 2BAFD 16

Translation of numbers with a base that is a power of 2 and reverse translation. Such SS include binary, octal, hexadecimal number systems.



Rule. Convert from binary SS to octal SS. The binary number is divided into groups of 3 digits from the end (from right to left) and each group is converted into a number in a new SS

10.000.101 2 =205 8

111.000.101.100 2 =7054 8

1.011.001.101 2 =1315 8

Rule. For the reverse conversion, each octal digit is written as a triad.

Rule. From binary SS to hexadecimal SS: similar, but separate 4 digits each

0110.0110.1011 2 =66B 16

1011.1111.0111 2 =BF7 16

10.1010.0111.0001 2 =2A71 16

Rule. For the reverse conversion, each hexadecimal digit is written as a tetrad.

Translation of proper and improper fractions in different SS. If you need to convert a fraction, you must first convert it to a decimal, and then apply the rules for converting decimal fractions.

Rule. Converting decimal fractions less than one (proper fractions).

1) it is necessary to separate the fractional part with a vertical line;

2) multiply the fractional part based on the new number system;

3) write the result strictly under the original number, starting from the least significant digit; if you get a transfer to an entire part, then write it to the left of the line;

4) multiplication of the fractional part is carried out until a number with the specified accuracy is obtained, or there is no 0 to the right of the line.

0,728 10 =0,564 8

Task 4. Convert the following proper fractions from decimal SS to binary, octal, hexadecimal SS: .