• Converting integers from one system to another. Quickly convert a number from the decimal number system to binary

    When converting numbers from decimal system notation to any other, the whole and fractional parts are always translated separately (according to different rules).

    Translation of the whole part

    In order to convert a number from a decimal number system to any other, you need to perform integer division of the original number by the base of the number system to which you want to convert the number. In this case, the remainder of the division and the quotient are important. The quotient must be divided by the base until 0 remains. After this, all remainders must be written in reverse order - this will be the number in new system Reckoning.

    For example, converting the number 25 from the decimal number system to the binary system will look like this:

    Writing out the remainders in reverse order, we get 25 10 =11001 2.

    If you think about it, you can easily notice that when converting absolutely any number into binary system In notation, the very last remainder (that is, the very first digit in the result) will always be equal to the very last quotient, which turned out to be less than the base of the number system into which we are converting the number. Therefore, division is often stopped before the quotient becomes equal to zero - at the moment when the quotient becomes simply less than the base. For example:

    Conversion from the decimal number system to any other number system is carried out according to exactly the same rules. Here is an example of converting 393 10 to hexadecimal number system:

    Writing out the remainders in reverse order, we get 393 10 =189 16.

    You need to understand that the remainders are obtained in the decimal number system. When dividing by 16, residues may appear not only from 0 to 9, but also residues from 10 to 15. Each remainder is always exactly one digit in the number system into which the translation is made.

    For example, if, when converting to the hexadecimal number system, you received the following remainders (written out in the order they should be written in the number): 10, 3, 15, 7, then in the hexadecimal number system this sequence of remainders will correspond to the number A3F7 16 (some they mistakenly write the number as 103157 16 - it is clear that this is a completely different number, and that if you do this, it turns out that the numbers from A to F will not appear in any hexadecimal number).

    Fractional translation

    When translating a fractional part, unlike translating an entire part, you do not need to divide, but multiply by the base of the number system into which we are converting. In this case, whole parts are discarded each time, and fractional parts are multiplied again. By collecting the whole parts in the order in which they were obtained, we obtain the fractional part of the number in required system Reckoning.

    One multiplication operation gives exactly one additional sign in the number system into which the translation is carried out.

    In this case, there are two conditions for completing the process:

    1) as a result of the next multiplication you received a zero in the fractional part. It is clear that no matter how much you multiply this zero, it will still remain zero. This means that the number has been converted from the decimal number system to the required one exactly.

    2) not all numbers can be translated accurately. In this case, it is usually translated with some accuracy. In this case, they first determine how many decimal places will be needed - this is the number of times that the multiplication operation will need to be performed.

    Here is an example of converting the number 0.39 10 to the binary number system. Accuracy - 8 digits (in in this case translation accuracy is chosen arbitrarily):

    If we write out the whole parts in direct order, we get 0.39 10 =0.01100011 2 .

    There is no need to write out the very first zero (crossed out in blue in the figure) - since it refers not to the fractional part, but to the whole part. Some people mistakenly write this zero after the decimal point when writing out the result.

    This is what the conversion of the number 0.39 10 to hexadecimal number system will look like. Accuracy - 8 digits; in this case, the accuracy is again chosen arbitrarily:

    If we write out the whole parts in direct order, we get 0.39 10 =0.63D700A3 16.

    At the same time, you probably noticed that whole parts when multiplied are obtained in the decimal number system. These integer parts obtained when translating the fractional part of a number should be interpreted in exactly the same way as the remainders when translating the integer part of a number. That is, if, when converted to the hexadecimal number system, the integer parts turned out in this order: 3, 13, 7, 10, then the corresponding number will be equal to 0.3D7A 16 (and not 0.313710 16, as some sometimes mistakenly write).

    Converting numbers with integer and fractional parts

    To translate a number with an integer and a fractional part, you need to translate the integer part separately, and the fractional part separately, and then write these two parts together.

    For example, 25.39 10 =11001.01100011 2 (translations of the integer and fractional parts - see above).

    Converting small integers from decimal to binary in your head

    Since when working with various number systems, especially when developing programs, the need to translate small integers very often arises, then, generally speaking, it makes sense to memorize the first 16 numbers (from 0 to 15).

    But if you figure out how easy it is to mentally convert small integers from 0 to 15 from the decimal number system to binary, then you can simply calculate a significant part of the table in your head every time you need it. Do this operation many times, and at some point you yourself will not be able to understand whether you have already memorized the table or are still calculating.

    So, to convert a small positive integer from 0 to 15 from decimal to binary, the first thing you need to understand is that each position in the binary number corresponds to a power of two. At the same time, powers of two for positions from 0 to 3 are very easy to remember - these are the numbers 1, 2, 4 and 8:

    And the number 10 is 2 plus 8:

    Well, the number 0 is a sin not to remember, since to get it you don’t need to add anything.

    When you are setting up networks various scales and every day you come across calculations - it’s not necessary to create this kind of cheat sheet, everything is already done on an unconditional reflex. But when you poke around in networks very rarely, you don’t always remember what the mask is in decimal form for the prefix 21 or what the network address is for the same prefix. In this regard, I decided to write several small articles-cheat sheets on converting numbers into various systems calculations, network addresses, masks, etc. In this part we will talk about converting numbers into different number systems.

    1. Number systems

    When you are doing something related to computer networks and IT, you will come across this concept anyway. And as a smart IT professional, you need to understand this at least a little, even if in practice you will use it very rarely.
    Let's look at the translation of each digit from an IP address 98.251.16.138 V following systems calculations:

    • Binary
    • Octal
    • Decimal
    • Hexadecimal

    1.1 Decimal

    Since the numbers are written in decimal, we will skip the conversion from decimal to decimal :)

    1.1.1 Decimal → Binary

    As we know, the binary number system is used in almost all modern computers and many other computing devices. The system is very simple - we only have 0 and 1.
    To convert a number with a tithe into binary form, you need to use division modulo 2 (i.e. integer division by 2), as a result of which we will always have a remainder of either 1 or 0. In this case, the result is written from right to left. An example will put everything in its place:


    Figure 1.1 – Converting numbers from decimal to binary


    Figure 1.2 – Converting numbers from decimal to binary system

    I will describe the division of the number 98. We divide 98 by 2, as a result we have 49 and the remainder 0. Next we continue division and divide 49 by 2, as a result we have 24 with a remainder 1. And in the same way we get to 1 or 0 in divisible. Then we write the result from right to left.

    1.1.2 Decimal → Octal

    The octal system is an integer number system with base 8. I.e. all numbers in it are represented in the range 0 – 7 and to convert from the decimal system you need to use division modulo 8.


    Figure 1.3 – Converting numbers from decimal to octal system

    The division is similar to the 2-point system.

    1.1.3 Decimal → Hexadecimal

    The hexadecimal system has almost completely replaced the octal system. It has a base of 16, but uses decimal digits from 0 to 9 + Latin letters from A (number 10) to F (number 15). You encounter it every time you check your settings. network adapter- this is the MAC address. Same when IPv6 is used.


    Figure 1.4 – Converting numbers from decimal to hexadecimal

    1.2 Binary

    In the previous example, we converted all decimal numbers to other number systems, one of which is binary. Now let's convert each number from binary form.

    1.2.1 Binary → Decimal

    To convert numbers from binary to decimal, you need to know two nuances. The first is that each zero and one have a multiplier of 2 in nth degree, in which n increases from right to left by exactly one. The second is that after multiplying, all the numbers need to be added and we will get the number in decimal form. As a result, we will have a formula like this:

    D = (a n × p n-1) + (a n-1 × p n-2) + (a n-2 × p n-3) +…, (1.2.1)

    Where,
    D is the decimal number we are looking for;
    n– the number of characters in a binary number;
    a – number in binary form on nth position(i.e. first character, second, etc.);
    p – coefficient equal to 2.8 or 16 to the power n(depending on the number system)

    For example, let’s take the number 110102. We look at the formula and write:

    • The number consists of 5 characters ( n=5)
    • a 5 = 1, a 4 = 1, a 3 = 0, a 2 = 1, a 1 = 0

    • p = 2 (since we are converting from binary to decimal)

    As a result we have:

    D = (1 × 2 5-1) + (1 × 2 5-2) + (0 × 2 5-3) + (1 × 2 5-4) + (0 × 2 5-5) = 16 + 8 + 0 + 2 + 0 = 26 10

    For those who are used to writing from right to left, the form will look like this:

    D = (0 × 2 5-5) + (1 × 2 5-4) + (0 × 2 5-3) + (1 × 2 5-2) + (1 × 2 5-1) = 0 + 2 + 0 + 8 + 16 = 26 10

    But, as we know, rearranging the terms does not change the sum. Let's now convert our numbers to decimal form.


    Figure 1.5 – Converting numbers from binary to decimal system

    1.2.2 Binary → Octal

    When translating, we need to divide the binary number into groups of three characters from right to left. If the last group does not consist of three characters, then we simply replace the missing bits with zeros. For example:

    10101001 = 0 10 101 001

    1011100 = 00 1 011 100

    Each group of bits is one of the octal numbers. To find out which one, you need to use the formula 1.2.1 written above for each group of bits. As a result we get.


    Figure 1.6 – Converting numbers from binary to octal system

    1.2.3 Binary → Hexadecimal

    Here we need to split the binary number into groups of four characters from right to left, followed by adding zeros to the missing bits of the group, as described above. If the last group consists of zeros, then they should be ignored.

    110101011 = 000 1 1010 1011

    1011100 = 0 101 1100

    001010000 = 00 0101 0000 = 0101 0000

    Each group of bits is one of the hexadecimal numbers. We use formula 1.2.1 for each group of bits.


    Figure 1.7 – Converting numbers from binary to hexadecimal

    1.3 Octal

    In this system, we may have difficulties only when converting to hexadecimal, since the rest of the translation goes smoothly.

    1.3.1 Octal → Binary

    Each number in the octal system is a group of three bits in the binary system, as described above. To translate, we need to use a cheat sheet:


    Figure 1.8 – Spur for converting numbers from the octal system

    Using this tablet, we will convert our numbers to the binary system.


    Figure 1.9 – Converting numbers from octal to binary

    I'll describe the conclusion a little. Our first number is 142, which means there will be three groups of three bits each. We use the spur and see that number 1 is 001, number 4 is 100 and number 2 is 010. As a result, we have the number 001100010.

    1.3.2 Octal → Decimal

    Here we use formula 1.2.1 only with a coefficient of 8 (i.e. p=8). As a result we have


    Figure 1.10 – Converting numbers from octal to decimal system

    • The number consists of 3 characters ( n=3)
    • a 3 = 1, a 2 = 4, a 1 = 2

    • p = 8 (since we are converting from octal to decimal)

    As a result we have:

    D = (1 × 8 3-1) + (4 × 8 3-2) + (2 × 8 3-3) = 64 + 32 + 2 = 98 10

    1.3.3 Octal → Hexadecimal

    As was written earlier, to translate, we first need to convert the numbers into the binary system, then from binary to hexadecimal, dividing them into groups of 4 bits. You can use the following spur.


    Figure 1.11 – Spur for converting numbers from the hexadecimal system

    This table will help you convert from binary to hexadecimal. Now let's convert our numbers.


    Figure 1.12 – Converting numbers from octal to hexadecimal

    1.4 Hexadecimal

    This system has the same problem when converting to octal. But more on that later.

    1.4.1 Hex → Binary

    Each number in hexadecimal is a group of four bits in binary, as described above. To translate, we can use the cheat sheet located above. As a result:


    Figure 1.13 – Converting numbers from hexadecimal to binary

    Let's take the first number - 62. Using the table (Fig. 1.11) we see that 6 is 0110, 2 is 0010, as a result we have the number 01100010.

    1.4.2 Hex → Decimal

    Here we use formula 1.2.1 only with a coefficient of 16 (i.e. p=16). As a result we have


    Figure 1.14 – Converting numbers from hexadecimal to decimal

    Let's take the first number. Based on formula 1.2.1:

    • The number consists of 2 characters ( n=2)
    • a 2 = 6, a 1 = 2

    • p = 16 (since we are converting from hexadecimal to decimal)

    As a result we have.

    D = (6 × 16 2-1) + (2 × 16 2-2) = 96 + 2 = 98 10

    1.4.3 Hex → Octal

    To convert to the octal system, you must first convert to binary, then divide it into groups of 3 bits and use the table (Fig. 1.8). As a result:


    Figure 1.15 – Converting numbers from hexadecimal to octal

    We will talk about IP addresses, masks and networks.

    Those taking the Unified State Exam and more...

    It is strange that in computer science lessons in schools they usually show students the most complex and inconvenient way to convert numbers from one system to another. This method consists of sequentially dividing the original number by the base and collecting the remainders from the division in reverse order.

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

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

    If you need to convert to the binary system, quite big numbers, then the division ladder takes on the size of a multi-story building. And how can you collect all the ones and zeros and not miss a single one?

    IN Unified State Exam program in computer science includes several tasks related to the translation of numbers from one system to another. Typically, this is a conversion between octal and hexadecimal 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 would be good to know by heart for those who choose computer science as their future profession.

    Table of powers 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 of these numbers, it is not difficult to obtain the rest in your mind from those that you remember.

    Table of binary numbers from 0 to 15 with hexadecimal representation:

    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 conversion

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

    1. We are looking for the power of two closest to 810 and not exceeding it. This is 2 9 = 512.
    2. Subtract 512 from 810, we get 298.
    3. Repeat steps 1 and 2 until there are no 1s or 0s left.
    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 methods, you can use any of them. How easy it is to see that in any number system its base is always 10. The square of the base will always be 100, the cube 1000. That is, the degree of the base of the number system is 1 (one), and there are as many zeros behind it as the degree is.

    Method 1: Arrange 1 according to the ranks of the indicators of the terms. In our example, these are 9, 8, 5, 3 and 1. The remaining places will contain zeros. So, we got the binary representation of the number 810 10 = 1100101010 2. Units are placed in 9th, 8th, 5th, 3rd and 1st places, counting from right to left from zero.

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

    810 =

    Now let's add these steps together, like folding a fan: 1100101010.

    That's it. Along the way, the problem of “how many units in binary notation number 810?

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

    Now the example is simpler.

    Let's convert the number 63 to the 5-ary number system. The closest power of 5 to 63 is 25 (square 5). A cube (125) will already be a lot. That is, 63 lies between the square of 5 and the cube. Then we will 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 hexadecimal systems. Since their base is a power of two, the translation is done automatically, simply by replacing the numbers 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.

    Let's convert the number 547 8 to binary.

    547 8 = 101 100 111
    5 4 7

    One more, for example 7D6A 16.

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

    Let's convert the number 7368 to the hexadecimal system. First, write the numbers in triplets, and then divide them into quadruples from the end: 736 8 = 111 011 110 = 1 1101 1110 = 1DE 16. Let's convert the number C25 16 to the octal 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. It is not difficult, the main thing is not to make mistakes in the calculations. We expand the number into a polynomial with powers of the base and coefficients for 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 .

    Converting Negative Numbers

    Here you need to take into account that the number will be presented in additional code. To convert a number into additional code, you need to know the final size of the number, that is, what we want to fit it into - in a byte, in two bytes, in four. The most significant digit of a number means the sign. If there is 0, then the number is positive, if 1, then it is 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 information.

    For translation negative number in binary's complement code you need to convert a positive number to binary, then change the zeros to ones and the 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 convert 79 to the binary system, 79 = 1001111. We add zeros on the left to the size of the byte, 8 bits, we get 01001111. We 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 Unified State Exam question “how many units are in the binary representation of the number -79?” The answer is 4.

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

    Converting fractional numbers

    Fractional numbers are converted in the reverse way of dividing whole numbers by the base, which we looked at at the very beginning. That is, using sequential multiplication by a new base with the collection of whole parts. The integer 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 integer and fractional parts are translated separately and then glued together.

    Let's convert the number 0.6752 to the binary system.

    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 the 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.

    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 ones 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. So as not to be new symbols, first ten digits of hexadecimal systems are designated by ordinary numbers, and the remaining six are in 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.

    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 every 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 translation into 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.

    Let's consider decimal number to binary system using the method of sequential division by 2. To convert a decimal number 25 into the code, you need to divide it by 2 until 0 remains. The remainders obtained at each division step are written in a line from right to left, after writing the digit of the last remainder this will be the final

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

    1. For translation binary number in decimal it is necessary to write it in the form of a polynomial, consisting of the products of the digits of a 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 in the form of 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 the number to the decimal number system.

    3. For translation hexadecimal number in decimal 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 sequentially divided by 2 until a remainder less than or equal to 1 remains. A number in the binary system is written as a sequence of the last division result and the remainders from the 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 remains 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.