• Converting numbers to binary, hexadecimal, decimal, octal number systems. Number systems and conversion from binary to decimal

    Hello, site visitor! We continue to study the IP network layer protocol, and to be more precise, its version IPv4. At first glance the topic binary numbers and binary number system has nothing to do with the IP protocol, but if we remember that computers work with zeros and ones, then it turns out that the binary system and its understanding is the basis of the fundamentals, we need learn to convert numbers from binary to decimal and vice versa: decimal to binary. This will help us better understand the IP protocol, as well as the principle of operation of variable-length network masks. Let's get started!

    If the topic of computer networks is interesting to you, you can read other course recordings.

    4.4.1 Introduction

    Before we begin, it’s worth explaining why a network engineer needs this topic. Although you could be convinced of its necessity when we spoke, you can say that there are IP calculators that greatly facilitate the task of allocating IP addresses, calculating the necessary subnet/network masks and determining the network number and host number in IP. address. That’s right, but the IP calculator is not always at hand, this is the reason number one. Reason number two is that in the Cisco exams they won't give you an IP calculator and that's it. you will have to do the conversion of IP addresses from decimal to binary on a piece of paper, and there are not so few questions where this is required in the exam/exams for obtaining the CCNA certificate, it would be a shame if the exam was failed because of such a trifle. And finally, understanding the binary number system leads to a better understanding of the principle of operation.

    In general, a network engineer is not required to be able to convert numbers from binary to decimal and vice versa in his head. Moreover, rarely anyone knows how to do this mentally; teachers of various courses on computer networks mainly fall into this category, since they constantly encounter this every day. But with a piece of paper and a pen, you should learn how to translate.

    4.4.2 Decimal digits and numbers, digits in numbers

    Let's start simple and talk about binary digits and numbers, you know that numbers and numbers are two different things. A number is a special symbol for designation, and a number is an abstract notation for quantity. For example, to write down that we have five fingers on our hand, we can use Roman and Arabic numerals: V and 5. In this case, five is both a number and a digit. And, for example, to write the number 20 we use two digits: 2 and 0.

    In total, in the decimal number system we have ten digits or ten symbols (0,1,2,3,4,5,6,7,8,9), by combining which we can write different numbers. What principle are we guided by when using the decimal number system? Yes, everything is very simple, we raise ten to one degree or another, for example, let’s take the number 321. How can it be written differently, like this: 3*10 2 +2*10 1 +1*10 0 . Thus, it turns out that the number 321 represents three digits:

    1. The number 3 means the most significant place or in this case it is the hundreds place, otherwise their number.
    2. The number 2 is in the tens place, we have two tens.
    3. The number one refers to the least significant digit.

    That is, in this entry a two is not just a two, but two tens or two times ten. And three is not just three, but three times a hundred. The following dependence is obtained: the unit of each next digit is ten times greater than the unit of the previous one, because what 300 is is three times a hundred. A digression regarding the decimal number system was necessary to make it easier to understand the binary system.

    4.4.3 Binary digits and numbers, as well as their recording

    There are only two digits in the binary number system: 0 and 1. Therefore, writing a number in the binary system is often much larger than in the decimal system. With the exception of the numbers 0 and 1, zero in the binary number system is equal to zero in the decimal number system, and the same is true for one. Sometimes, in order not to confuse which number system the number is written in, sub-indices are used: 267 10, 10100 12, 4712 8. The number in the sub-index indicates the number system.

    The symbols 0b and &(ampersand) can be used to write binary numbers: 0b10111, &111. If in the decimal number system, to pronounce the number 245 we use this construction: two hundred and forty-five, then in the binary number system, to name the number, we need to pronounce a digit from each digit, for example, the number 1100 in the binary number system should not be pronounced as a thousand one hundred, but like one, one, zero, zero. Let's look at writing the numbers from 0 to 10 in the binary number system:

    I think the logic should be clear by now. If in the decimal number system for each digit we had ten options available (from 0 to 9 inclusive), then in the binary number system in each of the digits of a binary number we have only two options: 0 or 1.

    To work with IP addresses and subnet masks, we only need natural numbers in the binary number system, although the binary system allows us to write fractional and negative numbers, but we don’t need this.

    4.4.4 Converting numbers from decimal to binary

    Let's take a better look at this how to convert a number from decimal to binary. And here everything is actually very, very simple, although it’s difficult to explain in words, so I’ll give it right away example of converting numbers from decimal to binary. Let's take the number 61, to convert to the binary system, we need to divide this number by two and see what is the remainder of the division. And the result of division is again divided by two. In this case, 61 is the dividend, we will always have two as a divisor, and we divide the quotient (the result of division) by two again, continue dividing until the quotient contains 1, this last unit will be the leftmost digit . The picture below demonstrates this.

    Please note that the number 61 is not 101111, but 111101, that is, we write the result from the end. In the latter particular, there is no sense in dividing the unit by two, since in this case integer division is used, and with this approach it turns out as in Figure 4.4.2.

    This is not the fastest way to convert a number from binary to decimal.. We have several accelerators. For example, the number 7 in binary is written as 111, the number 3 as 11, and the number 255 as 11111111. All these cases are incredibly simple. The fact is that the numbers 8, 4, and 256 are powers of two, and the numbers 7, 3, and 255 are one less than these numbers. So, for numbers that are one less than a number equal to a power of two, a simple rule applies: in the binary system, such a decimal number is written as a number of units equal to a power of two. So, for example, the number 256 is two to the eighth power, therefore, 255 is written as 11111111, and the number 8 is two to the third power, and this tells us that 7 in the binary number system will be written as 111. Well, understand, how to write 256, 4 and 8 in the binary number system is also not difficult, just add one: 256 = 11111111 + 1 = 100000000; 8 = 111 + 1 = 1000; 4 = 11 + 1 = 100.
    You can check any of your results on a calculator and it’s better to do so at first.

    As you can see, we have not yet forgotten how to divide. And now we can move on.

    4.4.5 Converting numbers from binary to decimal

    Converting numbers from binary is much easier than converting from decimal to binary. As an example of translation, we will use the number 11110. Pay attention to the table below, it shows the power to which you need to raise two in order to eventually get a decimal number.

    To get a decimal number from this binary number, you need to multiply each number in the digit by two to the power, and then add the results of the multiplication, it’s easier to show:

    1*2 4 +1*2 3 +1*2 2 +1*2 1 +0*2 0 = 16+8+4+2+0=30

    Let's open the calculator and make sure that 30 in the decimal number system is 11110 in binary.

    We see that everything was done correctly. From the example it is clear that Converting a number from binary to decimal is much easier than converting it back. To work with confidence you just need to remember powers of two up to 2 8. For clarity, I will provide a table.

    We don’t need more, since the maximum possible number that can be written in one byte (8 bits or eight binary values) is 255, that is, in each octet of the IP address or IPv4 subnet mask, the maximum possible value is 255. There are fields , in which there are values ​​greater than 255, but we do not need to calculate them.

    4.4.6 Addition, subtraction, multiplication of binary numbers and other operations with binary numbers

    Let's now look at operations that can be performed on binary numbers. Let's start with simple arithmetic operations and then move on to Boolean algebra operations.

    Adding binary numbers

    Adding binary numbers is not that difficult: 1+0 =1; 1+1=0 (I’ll give an explanation later); 0+0=0. These were simple examples where only one digit was used, let's look at examples where the number of digits is more than one.
    101+1101 in the decimal system is 5 + 13 = 18. Let's count in a column.

    The result is highlighted in orange, the calculator says that we calculated correctly, you can check it. Now let's see why this happened, because at first I wrote that 1+1=0, but this is for the case when we have only one digit, for cases when there are more than one digits, 1+1=10 (or two in decimal), which is logical.

    Then look what happens, we perform additions by digits from right to left:

    1. 1+1=10, write zero, and one goes to the next digit.

    2. In the next digit we get 0+0+1=1 (this unit came to us from the result of addition in step 1).

    4. Here we have a unit only in the second number, but it has also been transferred here, so 0+1+1 = 10.

    5. Glue everything together: 10|0|1|0.

    If you’re lazy in a column, then let’s count like this: 101011+11011 or 43 + 27 = 70. What can we do here, but let’s look, because no one forbids us to make transformations, and changing the places of the terms does not change the sum, for the binary number system this rule is also relevant.

    1. 101011 = 101000 + 11 = 101000 + 10 + 1 = 100000 + 1000 + 10 + 1.
    2. 11011 = 11000 + 10 + 1 = 10000 + 1000 + 10 + 1.
    3. 100000 + 10000 + (1000 +1000) + (10+10) + (1+1).
    4. 100000 + (10000 + 10000) + 100 + 10.
    5. 100000 + 100000 +110
    6. 1000000 + 110.
    7. 1000110.

    You can check with a calculator, 1000110 in binary is 70 in decimal.

    Subtracting Binary Numbers

    Immediately an example for subtracting single-digit numbers in the binary number system, we didn’t talk about negative numbers, so we don’t take 0-1 into account: 1 – 0 = 1; 0 – 0 = 0; 1 – 1 = 0. If there are more than one digits, then everything is also simple, you don’t even need any columns or tricks: 110111 – 1000, this is the same as 55 – 8. As a result, we get 101111. And the heart stopped beating , where does the unit in the third digit come from (numbering from left to right and starting from zero)? It's simple! In the second digit of the number 110111 there is 0, and in the first digit there is 1 (if we assume that the numbering of digits starts from 0 and goes from left to right), but the unit of the fourth digit is obtained by adding two units of the third digit (you get a kind of virtual two) and from this For twos, we subtract one, which is in the zero digit of the number 1000, and 2 - 1 = 1, and 1 is a valid digit in the binary number system.

    Multiplying binary numbers

    It remains for us to consider the multiplication of binary numbers, which is implemented by shifting one bit to the left. But first, let's look at the results of single-digit multiplication: 1*1 = 1; 1*0=0 0*0=0. Actually, everything is simple, now let's look at something more complex. Let's take the numbers 101001 (41) and 1100 (12). We will multiply by column.

    If it is not clear from the table how this happened, then I will try to explain in words:

    1. It is convenient to multiply binary numbers in a column, so we write out the second factor under the first; if the numbers have different numbers of digits, it will be more convenient if the larger number is on top.
    2. The next step is to multiply all the digits of the first number by the lowest digit of the second number. We write the result of the multiplication below; we need to write it so that under each corresponding digit the result of the multiplication is written.
    3. Now we need to multiply all the digits of the first number by the next digit of the second number and write the result one more line below, but this result needs to be shifted one digit to the left; if you look at the table, this is the second sequence of zeros from the top.
    4. The same must be done for subsequent digits, each time moving one digit to the left, and if you look at the table, you can say that one cell to the left.
    5. We have four binary numbers that we now need to add and get the result. We recently looked at addition, there shouldn't be any problems.

    In general, the multiplication operation is not that difficult, you just need a little practice.

    Boolean algebra operations

    There are two very important concepts in Boolean algebra: true and false, the equivalent of which is zero and one in the binary number system. Boolean algebra operators expand the number of available operators over these values, let's take a look at them.

    Logical AND or AND operation

    The Logical AND or AND operation is equivalent to multiplying single-digit binary numbers.

    1 AND 1 = 1; 1 AND 0 = 1; 0 AND 0 = 0; 0 AND 1 = 0.

    1 AND 1 = 1 ;

    1 AND 0 = 1 ;

    0 AND 0 = 0 ;

    0 AND 1 = 0.

    The result of “Logical AND” will be one only if both values ​​are equal to one; in all other cases it will be zero.

    Operation "Logical OR" or OR

    The operation “Logical OR” or OR works on the following principle: if at least one value is equal to one, then the result will be one.

    1 OR 1 = 1; 1 OR 0 = 1; 0 OR 1 = 1; 0 OR 0 = 0.

    1 OR 1 = 1 ;

    1 OR 0 = 1 ;

    0 OR 1 = 1 ;

    0 OR 0 = 0.

    Exclusive OR or XOR operation

    The operation "Exclusive OR" or XOR will give us a result of one only if one of the operands is equal to one and the second is equal to zero. If both operands are equal to zero, the result will be zero and even if both operands are equal to one, the result will be zero.

    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 precious 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 decimal representation in pink on the left.


    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 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 of conversion of 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 number 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:


    Received 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



    The most common calculation methods in the modern world are decimal and binary. They are used in completely different areas, but both are equally important. Often a conversion from binary to decimal system or vice versa is required. The names come from the bases, which depend on how many signs are used in writing numbers. In binary it is only 0 and 1, and in decimal it is from 0 to 9. In other systems, in addition to numbers, letters, other icons 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, we will primarily talk about the two already mentioned. It's actually amazing how all this could have been invented. Let's talk about this topic separately.

    History of origin

    Even now, when it would seem that the whole world thinks the same, there are a variety of different 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 contact each other, so a huge number of 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 often count in tens rather than dozens. All these are 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 duodecimal systems arose. But how did binary arise? Very simple and logical. The fact is that, for example, diodes have only two positions: it can be either on or off. The first state can thus 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.

    Areas of application

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

    Differences and features

    When it comes to number systems, it is imperative to somehow differentiate between them. After all, it is absolutely impossible to distinguish between 11 and 100 in different recording methods. That is why the pointer below and to the right of the number itself is used. So, when you see the entry 11 2 or 100 10, you can understand what we are talking about. Both systems are positional, that is, its value depends on the location of a particular digit. They talk about the digits of the decimal system in school: there are units, tens, hundreds, thousands, etc. In the binary system everything is the same. But due to the fact that its base - 2 - is less than 10, 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 decimal, which is the most common, reading occurs in a special way. If base 10 makes it possible to read 101 as "one hundred and one", then for 2 it will be "one zero one".

    Returning to the issue of discharges, it must be repeated that due to a much smaller base, more discharges are required. So, for example, 8 10 is 1000 2. The difference is obvious - one rank and four. Another major difference is that there are no negative numbers 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 made?

    Algorithm

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

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

    3) add up the results.

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

    In order to carry out the reverse operation, that is, move from the decimal system to the binary system, you need to do this:

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

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

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

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

    Fractions

    Fortunately or unfortunately, the fact remains that the binary system uses not only integers. Converting fractions is not a very difficult, but often time-consuming task for humans. If the original number is presented in the decimal system, then after converting the integer, everything after the decimal point should no longer be divided, but multiplied by 2, writing down the integer parts. If you are converting from binary to decimal system, then everything is even simpler. In this case, when the decimal part conversion begins, the power to which 2 is raised will successively be -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 reinforce theory, so it would be best to consider the following examples:

    • converting 1000101 2 to the 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 the 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 only the binary and decimal systems as an example, it becomes clear that changing the base manually is a non-trivial task. But there are also others: hexadecimal, octal, sexagesimal, etc. When manually converting from one number system to another, care is extremely necessary. It’s really difficult not to get confused, especially if the post is long. In addition, we must not forget that digits are counted from 0, not 1, that is, the number of digits will always be one more. Of course, you need to carefully count the number of digits and not make mistakes in arithmetic operations and, of course, not skip steps in the algorithm. Ultimately, there are ways to transition between bases using software methods. But here it’s easier to write a script yourself than to search for it on the World Wide Web. In any case, manual translation skills, as well as a theoretical understanding of how this is done, should also be present.

    Instructions

    Video on the topic

    In the counting system that we use every day, there are ten digits - from zero to nine. That's why it's called decimal. However, in technical calculations, especially those related to computers, other systems, specifically binary and hexadecimal. Therefore you need to be able to translate numbers from one systems counting to another.

    You will need

    • - a piece of paper;
    • - pencil or pen;
    • - calculator.

    Instructions

    The binary system is the simplest. It has only two digits - zero and one. Each digit of binary numbers, starting from the end, corresponds to a power of two. Two in equals one, in the first - two, in the second - four, in the third - eight, and so on.

    Suppose you are given the binary number 1010110. The units in it are in second, third, fifth and seventh places. Therefore, in the decimal system this number is 2^1 + 2^2 + 2^4 + 2^6 = 2 + 4 + 16 + 64 = 86.

    Inverse problem - decimal numbers system. Let's say you have the number 57. To get it, you must sequentially divide the number by 2 and write the remainder. The binary number will be built from end to beginning.
    The first step will give you the last digit: 57/2 = 28 (remainder 1).
    Then you get the second one from the end: 28/2 = 14 (remainder 0).
    Further steps: 14/2 = 7 (remainder 0);
    7/2 = 3 (remainder 1);
    3/2 = 1 (remainder 1);
    1/2 = 0 (remainder 1).
    This is the last step because the result of division is zero. As a result, you got the binary number 111001.
    Check your answer: 111001 = 2^0 + 2^3 + 2^4 + 2^5 = 1 + 8 + 16 + 32 = 57.

    The second, used in computer matters, is hexadecimal. It has not ten, but sixteen digits. To avoid new conventions, the first ten digits of hexadecimal systems are designated by ordinary numbers, and the remaining six - by Latin letters: A, B, C, D, E, F. They correspond to decimal notation numbers m from 10 to 15. To avoid confusion, the number written in hexadecimal is preceded by the # sign or the symbols 0x.

    To make a number from hexadecimal systems, you need to multiply each of its digits by the corresponding power of sixteen and add the results. For example, the number #11A in decimal notation is 10*(16^0) + 1*(16^1) + 1*(16^2) = 10 + 16 + 256 = 282.

    Reverse conversion from decimal systems to hexadecimal is done using the same method of remainders as to binary. For example, take the number 10000. Consistently dividing it by 16 and writing down the remainders, you get:
    10000/16 = 625 (remainder 0).
    625/16 = 39 (remainder 1).
    39/16 = 2 (remainder 7).
    2/16 = 0 (remainder 2).
    The result of the calculation will be the hexadecimal number #2710.
    Check your answer: #2710 = 1*(16^1) + 7*(16^2) + 2*(16^3) = 16 + 1792 + 8192 = 10000.

    Transfer numbers from hexadecimal systems It's much easier to convert to binary. The number 16 is a two: 16 = 2^4. Therefore, each hexadecimal digit can be written as a four-digit binary number. If you have less than four digits in a binary number, add leading zeros.
    For example, #1F7E = (0001)(1111)(0111)(1110) = 1111101111110.
    Check the answer: both numbers in decimal notation they are equal to 8062.

    To translate, you need to break the binary number into groups of four digits, starting from the end, and replace each such group with a hexadecimal digit.
    For example, 11000110101001 becomes (0011)(0001)(1010)(1001), which in hexadecimal notation is #31A9. The correctness of the answer is confirmed by conversion to decimal notation: both numbers are equal to 12713.

    Tip 5: How to convert a number to binary

    Due to its limited use of symbols, the binary system is most convenient for use in computers and other digital devices. There are only two symbols: 1 and 0, so this system used in the operation of registers.

    Instructions

    Binary is positional, i.e. The position of each digit in a number corresponds to a certain digit, which is equal to two to the appropriate power. The degree starts at zero and increases as you move from right to left. For example, number 101 is equal to 1*2^0 + 0*2^1 + 1*2^2 = 5.

    Octal, hexadecimal and decimal systems are also widely used among positional systems. And if for the first two the second method is more applicable, then for translation from both are applicable.

    Consider a decimal number to binary system by sequential division by 2. To convert a decimal number 25 V