How to set up smartphones and PCs. Informational portal
  • home
  • Advice
  • Converting numbers to different number systems with a solution. How to convert from binary to decimal

Converting numbers to different number systems with a solution. How to convert from binary to decimal

Most people on our planet use the decimal number system for counting, but computers use the binary system. Some tribes at the dawn of the development of mankind used the duodecimal and sixtiesfold. It is from them that we have 12 hours left on the dial and 60 minutes per hour.

Sometimes it is necessary to transfer a number from one system to another. In this article, we will take a closer look at how to convert to the decimal system from some other popular systems.

The principle of constructing a number from digits

First of all, you need to understand what a number system is and its base. Number system is a way of representing numbers as a combination of certain numbers. The base of the system is the number of digits used in it. For example, in the base 10 decimal system there are only 10 digits - from 0 to 9. In the hexadecimal system, respectively, there are 16 digits, for which the Arabic numerals 0 - 9 and the Latin letters A - F are used instead of 10 - 15. For example, 2F7BE 16 - Hexadecimal number. With this notation, the subscript denotes the base of the number system. The key difference between systems with different bases is the "value" of 10. In hex, 10 16 is 16 10, and in binary 10 2 is just two. 100 16 will be calculated as

100 16 = 10 16 * 10 16 = 16 10 * 16 10 = 256 10 .

It is also necessary to distinguish between the concepts of "digit" and "number". A number is denoted by one symbol, and a number may be denoted by several. For example, the number 9 10 in the binary system will look like 1001 2, and the number 9 in the binary system does not exist as such.

Algorithm of translation

To convert a number to the decimal system, you need to learn how to apply a simple algorithm.

  1. Determine the base of the number system. It is indicated by a subscript after the number, for example, in the number 2F7BE 16, the base is 16.
  2. Multiply each digit of the number by the base to the power equal to the number of the digit from right to left, starting from zero. In the number 2F7BE, 16 E (equal to 14) is multiplied by 16 to the zero power, B (digit 11) by 16 to the first power, and so on: 2F7BE 16 = 2 * 16 4 + 15 * 16 3 + 7 * 16 2 + 11 * 16 1 + 14 * 16 0.
  3. Add up the results.

2*16 4 +15*16 3 + 7*16 2 + 11*16 1 + 14*16 0 = 194494 10 .

Let's look at examples of how the most popular - hexadecimal, octal and binary systems can be converted to decimal.

  • 5736 8 = 5*8 3 + 7*8 2 + 3*8 1 + 6*8 0 = 3038 10
  • 1001011 2 = 1*2 6 + 0*2 5 + 0*2 4 + 1*2 3 + 0*2 2 + 1*2 1 + 1*2 0 = 75 10
  • 2F7BE 16 = 2 * 16 4 + 15 * 16 3 + 7 * 16 2 + 11 * 16 1 + 14 * 16 0 = 194494 10

Of course, it is inconvenient, irrational, and reluctant to count manually each time. There are many calculators that can translate numbers from system to system. For example, a standard Windows calculator in Programmer mode (Alt + 3 keys or the View menu) can work with radix systems 2, 8, 10, and 16.

The positional number system first appeared in ancient Babylon. In India, the system works as

positional decimal numbering using zero, the Indians have a given system of numbers

borrowed by the Arab nation, from them, in turn, took the Europeans. In Europe, this system has become

call it Arabic.

Positional system - the meaning of all digits depends on the position (digit) of the given digit in the number.

Examples, the standard 10th number system is the positional system. Let's say the number 453 is given.

The number 4 stands for hundreds and corresponds to the number 400, 5 - the number of tens and corresponds to the value 50,

and 3 - one and the value 3. It is easy to see that the value increases with increasing discharge.

Thus, we write down the given number as the sum of 400 + 50 + 3 = 453.

Binary number system.

There are only 2 digits here - these are 0 and 1. Binary base- number 2.

The number that is located on the very edge to the right indicates the number of units, the second number -

In all digits, only one digit is possible - either zero or one.

Using the binary number system, it is possible to encode any natural number by representing

it is a number in the form of a sequence of zeros and ones.

Example: 10112 = 1 * 2 3 + 0 * 2 * 2 + 1 * 2 1 + 1 * 2 0 = 1 * 8 + 1 * 2 + 1 = 1110

The binary number system, like the decimal number system, is often used in computing

technique. The computer stores text and numbers in its memory in binary code and programmatically converts

into the image on the screen.

Addition, subtraction and multiplication of binary numbers.

Binary addition table:

10 (transfer to

senior rank)

Subtraction table in binary number system:

(loan from senior

discharge) 1

Column folding example (14 10 + 5 10 = 19 10 or 1110 2 + 101 2 = 10011 2):

+ 1 1 1 0
1 0 1
1 0 0 1 1

Binary multiplication table:

An example of multiplication "column" (14 10 * 5 10 = 70 10 or 1110 2 * 101 2 = 1000110 2):

* 1 1 1 0
1 0 1
+ 1 1 1 0
1 1 1 0
= 1 0 0 0 1 1 0

Converting numbers in binary notation.

To convert from binary to decimal use the following power table

bases 2:

Starting with the number one, each digit is multiplied by 2. The point after 1 is called binary point.

Convert binary numbers to decimal.

Let, there is a binary number 110001 2. To convert to decimal, write it down as a sum by

discharges as follows:

1 * 2 5 + 1 * 2 4 + 0 * 2 3 + 0 * 2 2 + 0 * 2 1 + 1 * 2 0 = 49

A little bit differently:

1 * 32 + 1 * 16 + 0 * 8 + 0 * 4 + 0 * 2 + 1 * 1 = 49

It is also good to write the calculation as a table:

We move from right to left. Under all binary units, we write its equivalent in the line below.

Convert binary fractions to decimal numbers.

Exercise: Convert the number 1011010, 101 2 to Decimal.

We write down the given number in this form:

1*2 6 +0*2 5 +1*2 4 +1*2 3 +0 *2 2 + 1 * 2 1 + 0 * 2 0 + 1 * 2 -1 + 0 * 2 -2 + 1 * 2 -3 = 90,625

Another recording option:

1*64+0*32+1*16+1*8+0*4+1*2+0*1+1*0,5+0*0,25+1*0,125 = 90,625

Or in the form of a table:

0.25

0.125

0.125

Convert decimal numbers to binary.

Suppose it is necessary to convert the number 19 to binary. We can do it this way:

19 /2 = 9 with the remainder 1

9 /2 = 4 with the remainder 1

4 /2 = 2 without a remainder 0

2 /2 = 1 without a remainder 0

1 /2 = 0 with the remainder 1

That is, each quotient is divided by 2 and the remainder is written to the end of the binary notation. Division

continues until the quotient does not contain zero. We write the result from right to left. Those. bottom

the number (1) will be on the far left, and so on. So, we got the number 19 in binary notation: 10011.

Convert fractional decimal numbers to binary numbers.

When an integer part is present in a given number, then it is converted separately from the fractional part. Translation

a fractional number from decimal to binary is as follows:

  • The fraction is multiplied by the base of the binary number system (2);
  • In the resulting work, the whole part is highlighted, which is taken as the senior

digit of a number in the binary number system;

  • The algorithm ends if the fractional part of the resulting product is zero or if

the required accuracy of calculations has been achieved. Otherwise, calculations continue over

fractional part of the work.

Example: You need to convert fractional decimal number 206.116 to binary fractional number.

Translating the whole part, we get 206 10 = 11001110 2. Fractional part 0.116 is multiplied by base 2,

we put the whole parts of the product in the digits after the decimal point:

0,116 . 2 = 0,232

0,232 . 2 = 0,464

0,464 . 2 = 0,928

0,928 . 2 = 1,856

0,856 . 2 = 1,712

0,712 . 2 = 1,424

0,424 . 2 = 0,848

0,848 . 2 = 1,696

0,696 . 2 = 1,392

0,392 . 2 = 0,784

Result: 206,116 10 ≈ 11001110,0001110110 2

Algorithm for converting numbers from one number system to another.

1. From the decimal number system:

  • divide the number by the base of the number system to be translated;
  • find the remainder of dividing the integer part of the number;
  • write down all the remainders of the division in reverse order;

2. From the binary number system:

  • to convert to the decimal number system, we find the sum of the products of the base 2 by

the appropriate degree of discharge;

In everyday life, we are used to using the decimal number system, familiar to us from school. However, in addition to it, there are many other systems. How to write numbers not in decimal, but, for example, in?

How to convert any decimal number to binary

The need to convert a decimal number to binary looks daunting only at first glance. In fact, it is quite simple - you do not even have to look for online services to complete the operation.

  • For a sample, let's take the number 156, written in the usual decimal form, and try to translate it into binary.
  • The algorithm will look like this - the initial number will need to be divided by two, then again by 2, and again by 2 until there is no one left in the answer.
  • When performing division for translation into binary code, it is not the integers that matter, but the remainders. If, when dividing the answer, an even number is obtained, then the remainder is written in the form of the number 0, if it is odd, then in the form of the number 1.
  • In practice, you can easily make sure that the initial binary series of the remainders for the number 156 will look like this - 00111001. In order to turn it into a full-fledged binary code, this series will need to be written in the reverse order - that is, 10011100.

The binary number 10011100, obtained as a result of a simple operation, will be the binary expression of the number 156.

Another example, but already in the picture

Binary to Decimal Conversion

The reverse translation - from binary to decimal - may seem a little more complicated. But if you use a simple doubling method, then you can cope with this task in a couple of minutes. For example, let's take the same number, 156, but in binary form - 10011100.

  • The doubling method is based on the fact that at each step of the calculation, they take the so-called previous total and add the next figure to it.
  • Since at the first step the previous total does not yet exist, here they always take 0, double it and add the first digit of the expression to it. In our example, this will be 0 * 2 + 1 = 1.
  • At the second step, we already have the previous total - it is equal to 1. This figure must be doubled, and then the next one in order must be added to it, that is - 1 * 2 + 0 = 2.
  • At the third, fourth and subsequent steps, the previous totals are still taken and added up with the subsequent figure in the expression.

When only one last digit remains in the binary notation, and there is nothing more to add, the operation will be completed. With the help of a simple check, you can make sure that the answer will get the desired decimal number 156.

The most common calculation methods in the modern world are decimal and binary. They are used in very different areas, but both are equally important. Often a conversion from binary to decimal or vice versa is also required. The names are derived from the radix, which depends on how many characters are used in the notation of numbers. In binary, these are only 0 and 1, and in decimal - from 0 to 9. In other systems, in addition to numbers, letters, other symbols and even hieroglyphs are used, but almost all of them have long been outdated. Since even other types of number systems are much less common, then we will focus primarily on the two already mentioned. It's actually amazing how all this could have been thought of. Let's talk about this separately.

History of origin

Even now, when, it would seem, the whole world thinks the same, there are a variety of systems. In the most remote corners of the globe, they are content with only the concepts of "one", "two" and "many", or something similar. What can we say about those times when it was much more difficult for people to communicate with each other, so a huge number of very different types of records and calculation methods were used. Humanity did not immediately come to the existing system, and this is reflected in the fact that the hour is divided into 60 minutes, and not into 100 periods of time, which would seem to be more logical. And at the same time, people are more likely to count in tens than in dozens. These are all echoes of the time when one's own fingers or, for example, the phalanges of some of them served as tools for quantifying something. This is how the decimal and twelve-digit systems arose. But how did binary come about? Very simple and logical. The fact is that, for example, diodes have only two positions: it can be either on or off. Thus, the first state can be written as 1, and the second as 0. However, this does not mean that the binary system arose simultaneously with electronic devices. It was used much earlier, for example, Leibniz considered it extremely convenient, elegant and simple. It’s even surprising that this number system did not eventually become the main one.

Applications

For most people, the two basic number systems simply do not overlap. So translating from binary to decimal is not a feasible task for everyone. The fact is that the latter system is used in everyday life, in communication between people, in simple calculations, etc. But all digital devices, primarily computers, speak the binary language. Any information in the memory of every desktop PC, tablet, phone, laptop and many other devices is a different combination of zeros and ones.

Differences and features

When it comes to number systems, it is imperative to somehow delimit them. After all, it is simply completely impossible to distinguish 11 or 100 in different recording methods. That is why a pointer is used below and to the right of the number itself. So, when you see the entry 11 2 or 100 10, you can understand what the speech is about. Both systems are positional, that is, its value depends on the place of a particular number. They talk about the digits of the decimal system at school: there are ones, tens, hundreds, thousands, etc. In binary, everything is the same. But due to the fact that its base - 2 - is less than 10, then it needs much more digits, that is, the recording of numbers turns out to be much longer. By the way, in binary, as in all other systems, except for decimal, as the most common, reading occurs in a special way. If radix 10 reads 101 as "one hundred and one", then for 2 it would be "one zero one".

Returning to the issue of discharges, it must be reiterated that due to the much lower base, more discharges are required. So, for example, 8 10 is 1000 2. The difference is obvious - one grade and four. Another major difference is that negative numbers do not exist in the binary system. Of course, you can write it down, but it will still be stored and encrypted differently. So how is the conversion from binary to decimal and vice versa done?

Algorithm

Rarely enough, but still sometimes you have to make the transition from one foundation to another. In other words, there is a need to translate from binary to decimal and vice versa. Modern computers do it quickly and easily, even if the records are very long and voluminous. Humans can do this too, albeit much more slowly and less efficiently. It is not so difficult to carry out both one and the second operation, but knowledge of how to do it, attentiveness and practice is required. In order to go from base 2 to base 10, you need to do the following steps:

2) sequentially multiply the value by 2 raised to the power equal to the position number;

3) add up the results.

Another way is to start summing the products of numbers sequentially from right to left. This is called the Horner transform and many find it more convenient than the usual algorithm.

In order to carry out the reverse operation, that is, to switch from the decimal system to the binary one, you need to do the following:

1) divide the original number by 2 and write down the remainder (1 or 0);

2) repeat step 1 until the moment when there is only 0 or 1;

3) write down the obtained values ​​in order.

There are other ways to convert from binary to decimal and vice versa. But they have no advantage over the described algorithm, they are not more efficient. But they require skills in performing arithmetic operations in a binary system, which is available to very few.

Fractions

Fortunately or unfortunately, the fact remains - not only integers are used in the binary system. Translation of fractions is not too difficult, but often time-consuming task for a person. If the initial number is represented in the decimal system, then after converting an integer, everything after the decimal point should no longer be divided, but multiplied by 2, writing down the whole parts. If you are converting from binary to decimal system, then everything is even easier. In this case, when the decimal point conversion starts, the degree to which 2 is raised will sequentially equal -1, -2, -3, etc. It is best to consider this in practice.

Example

In order to understand how to apply the described algorithms, you need to do all the operations yourself. Practice can always solidify a theory, so it is best to consider the following examples:

  • translation 1000101 2 into decimal system: 1x2 6 + 0x2 5 + 0x2 4 + 0x2 3 + 1x2 2 + 0x2 1 + 1x2 0 = 64 + 0 + 0 + 0 + 4 + 1 = 69 10;
  • using Horner's method. 00110111010 2 = 0x2 + 0 = 0x2 + 0 = 0x2 + 1 = 1x2 + 1 = 3x2 + 0 = 6x2 + 1 = 13x2 + 1 = 27x2 + 1 = 55x2 + 0 = 110x2 + 1 = 221x2 + 0 = 442 10;
  • 1110.01 2: 1x2 3 + 1x2 2 + 1x2 1 + 0x2 0 + 0x2 -1 + 1x2 -2 = 8 + 4 + 2 + 0.25 = 14.25 10;
  • from decimal system: 15 10 = 15/2 = 7 (1) / 2 = 3 (1) / 2 = 1 (1) / 2 = 0 (1) = 1111 2;

How not to get confused?

Even using the example of only binary and decimal systems, it becomes clear that changing the radix manually is a non-trivial task. But there are also others: hexadecimal, octal, hexadecimal, etc. Carefulness is extremely necessary when translating manually from one number system to another. It's really hard not to get confused, especially if the recording is long. In addition, we must not forget that the digits are counted from 0, not 1, that is, the number of digits will always be one more. Of course, you need to carefully calculate the number of digits and avoid errors in arithmetic operations and, of course, do not skip steps in the algorithm. Ultimately, there are ways to transition between bases programmatically. But here it is easier to write a script on your own than to search for it in the vastness of the world wide web. In any case, manual translation skills, as well as a theoretical understanding of how this is done, should also be available.

For those taking the exam and not only ...

It is strange that in computer science lessons in schools, students are usually shown the most difficult and inconvenient way to transfer numbers from one system to another. This method consists in sequentially dividing the original number by the base and collecting the remainders from the division in the reverse order.

For example, you need to convert the number 810 10 to the binary system:

We write the result in reverse order from bottom to top. It turns out 81010 = 11001010102

If you need to translate rather large numbers into the binary system, then the division ladder takes on the size of a multi-storey building. And how can you collect all ones with zeros and not miss a single one?

The USE program in computer science includes several tasks related to the translation of numbers from one system to another. Typically, this is a conversion between 8- and 16-ary systems and binary. These are sections A1, B11. But there are also problems with other number systems, such as in section B7.

To begin with, let us recall two tables that it would be good to know by heart for those who choose computer science as their future profession.

Power table of number 2:

2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10
2 4 8 16 32 64 128 256 512 1024

It is easily obtained by multiplying the previous number by 2. So, if you do not remember all these numbers, the rest are easy to get in your mind from those that you remember.

A table of binary numbers from 0 to 15 with hexadecimal notation:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
0 1 2 3 4 5 6 7 8 9 A B C D E F

The missing values ​​are also easy to calculate by adding 1 to the known values.

Integer translation

So, let's start by translating directly into the binary system. Let's take the same number 810 10. We need to expand this number into terms equal to powers of two.

  1. We are looking for the closest power of two to 810, not exceeding it. This is 2 9 = 512.
  2. Subtract 512 from 810 to get 298.
  3. Repeat steps 1 and 2 until 1 or 0 remains.
  4. We got it like this: 810 = 512 + 256 + 32 + 8 + 2 = 2 9 + 2 8 + 2 5 + 2 3 + 2 1.
Then there are two ways, you can use either of them. It is easy to see that in any number system, its base is always 10. The square of the base will always be 100, the cube is 1000. That is, the degree of the base of the number system is 1 (one), and behind it there are as many zeros as the degree.

Method 1: Arrange 1 in those categories, which are the indicators of the terms. In our example, these are 9, 8, 5, 3 and 1. Zeros will be used in other places. So, we got the binary representation of the number 810 10 = 1100101010 2. Units stand at 9th, 8th, 5th, 3rd and 1st places, counting from right to left from zero.

Method 2: Let us write the terms as powers of two under each other, starting with the largest.

810 =

Now let's put these steps together, as a fan folds: 1100101010.

That's all. Along the way, the problem "how many units are in the binary notation of the number 810?" Is also simply solved.

The answer is as many as there are terms (powers of two) in such a representation. 810 has 5 of them.

Now the example is simpler.

Let's translate the number 63 into the 5-ary number system. The closest power of 5 to 63 is 25 (square 5). The cube (125) will already be a lot. That is, 63 lies between square 5 and the cube. Then we select the coefficient for 5 2. This is 2.

We get 63 10 = 50 + 13 = 50 + 10 + 3 = 2 * 5 2 + 2 * 5 + 3 = 223 5.

And finally, very easy translations between 8- and 16-ary systems. Since their base is a power of two, the translation is done automatically, simply by replacing the digits with their binary representation. For the octal system, each digit is replaced by three binary digits, and for the hexadecimal system, four. In this case, all leading zeros are required, except for the most significant digit.

Converting the number 547 8 into binary system.

547 8 = 101 100 111
5 4 7

Another one, for example 7D6A 16.

7D6A 16 = (0)111 1101 0110 1010
7 D 6 A

Let's translate the number 7368 into the hexadecimal system. First, write the numbers in threes, and then divide them into fours from the end: 736 8 = 111 011 110 = 1 1101 1110 = 1DE 16. Let's translate the number C25 16 into the 8-ary system. First, we write the numbers in fours, and then divide them into threes from the end: C25 16 = 1100 0010 0101 = 110 000 100 101 = 6045 8. Now let's look at converting back to decimal. He does not represent labor, the main thing is not to be mistaken in the calculations. We decompose the number into a polynomial with the degrees of the base and the coefficients at them. Then we multiply and add everything. E68 16 = 14 * 16 2 + 6 * 16 + 8 = 3688. 732 8 = 7 * 8 2 + 3 * 8 + 2 = 474.

Translation of negative numbers

Here you need to take into account that the number will be represented in the's complement code. To translate a number into a complementary code, you need to know the final size of the number, that is, what we want to write it into - in a byte, in two bytes, in four. The most significant bit of a number means a sign. If there is 0, then the number is positive, if 1, then negative. On the left, the number is supplemented with a sign digit. We do not consider unsigned numbers, they are always positive, and the most significant bit in them is used as informational.

To convert a negative number to binary's complement code, you need to convert a positive number to the binary system, then change zeros to ones and ones to zeros. Then add 1 to the result.

So, let's convert the number -79 to the binary system. The number will take us one byte.

We translate 79 into the binary system, 79 = 1001111. Supplement the left with zeros to the size of a byte, 8 digits, we get 01001111. Change 1 to 0 and 0 to 1. We get 10110000. We add 1 to the result, we get the answer 10110001. Along the way, we answer the question of the exam "how many units in the binary representation of the number -79?" The answer is 4.

Adding 1 to the inverse of the number eliminates the difference between the representations +0 = 00000000 and -0 = 11111111. In two's complement, they will be written the same 00000000.

Fractional Numbers Translation

Fractional numbers are translated in the opposite way to the division of integers by the base, which we considered at the very beginning. That is, with the help of sequential multiplication by a new base, collecting whole parts. The whole parts obtained during multiplication are collected, but do not participate in the following operations. Only fractions are multiplied. If the original number is greater than 1, then the whole and fractional parts are translated separately, then glued together.

Converting the number 0.6752 to Binary.

0 ,6752
*2
1 ,3504
*2
0 ,7008
*2
1 ,4016
*2
0 ,8032
*2
1 ,6064
*2
1 ,2128

The process can be continued for a long time until we get all zeros in the fractional part or the required accuracy is achieved. Let's stop at the 6th sign for now.

It turns out 0.6752 = 0.101011.

If the number was 5.6752, then in binary it will be 101.101011.

Top related articles