Where is the binary decimal system used? Representation of numbers in binary code. Converting numbers: from binary to decimal

Binary decimal number system. The decimal digits 0 to 9 are replaced by the binary tetrads representing them: 0=0000, 1=0001, 2=0010, 3=0011, 4=0100, 5=0101, 6=0110, 7=0111, 8=1000 and 9= 1001. This notation is very often used as an intermediate step in converting a number from the decimal system to binary or vice versa. Since 10 is not an exact power of 2, not all 16 tetrads are used, and the algorithms for arithmetic operations on multi-digit numbers are more complex than in the basic number systems. And yet, the binary decimal number system is used even at this level in many microcalculators and some computers (in particular, Yamaha MSX).

Since representation and arithmetic in the decimal number system are most familiar to humans, and binary representation and binary arithmetic are most familiar to computers, a compromise system of binary-decimal notation of numbers was introduced. This system is most often used where there is a need to frequently use the decimal input/output procedure. (electronic watches, calculators, caller numbers, etc.). In such devices, it is not always advisable to provide universal microcode for converting binary numbers to decimal numbers and vice versa due to the small amount of program memory.

The principle of this system is quite simple: each decimal digit is converted directly to its 4-bit decimal equivalent, for example: 369110=0011 0110 1001 0001DEC:

Decimal 3 6 9 1 BCD 0011 0110 1001 0001

Let's convert the binary decimal number 1000 0000 0111 0010 to its decimal equivalent. Each group of 4 bits is converted to its decimal equivalent. We get 1000 0000 0111 0010DEC = 807210:

BCD 1000 0000 0111 0010 Decimal 8 0 7 2

Microprocessors use pure binary numbers, but also understand BCD conversion commands. The resulting binary decimal numbers are easily represented in decimal notation, which is more understandable to people.

Converting Binary Numbers to BCD

The arithmetic-logical unit of AVR microcontrollers (as well as other microprocessors) performs elementary arithmetic and logical operations on numbers represented in binary code. The ADC conversion results are read in binary code; it is convenient to process measurement results in binary code (in the format of integers or floating point numbers). However, when the final result is displayed on the indicator, it must be converted into a decimal format that is human-readable.

This section discusses programs for converting binary numbers to BCD.

1. Formats for representing decimal numbers

There are currently two common formats for representing decimal numbers in microprocessors - packed binary-coded decimal (BCD-Binary-Coded Decimal) and unpacked decimal code.

A packed BCD code is a representation of a decimal number where each decimal digit is represented by a 4-bit binary positional code 8-4-2-1. In this case, the byte contains two decimal digits. The least significant decimal digit occupies the right tetrad (bits 3: 0), the most significant decimal digit occupies the left tetrad (bits 7: 4). Multi-bit BCD numbers occupy several contiguous bytes. If the number is signed, then the most significant tetrad of the most significant byte is allocated to represent the sign in BCD format. Six binary code patterns can be used to encode a character, which are not used to represent decimal digits. These are codes 1010-1111 (A-F in hexadecimal). Typically, code 1100 (C) is used to encode the plus sign, and 1101 (D) for the minus sign.

Unpacked decimal code is a subset of the international ASCII character encoding table (Table 1). It can be seen that storing unpacked decimal numbers requires twice as much memory, since each digit is represented by an 8-bit code. Table 1: ASCII codes for decimal digits

2. Convert 16-bit integer numbers to BCD numbers

www.atmel.com offers a program "bin2bcd16" to convert 16-bit binary integer numbers to packed BCD numbers. This article discusses the program "bin16bcd5" (see Appendix, Program 1), written by A.V. Tereshkin according to the algorithm outlined in, and performing the same task. The latter program turned out to be more efficient than the first in terms of speed, code length and number of registers used.

The algorithm of the "bin16bcd5" program is as follows. Suppose we have an unsigned 16-bit integer number (range 0 to 65535). Obviously, you need to find 5 decimal digits. The method of conversion is to subtract 10,000 from the original number and first determine the decimal digit of tens of thousands. Then the thousands figure is found by successively subtracting the number 1000, etc. The subtraction is carried out each time until a negative difference is obtained by counting the number of subtractions. When moving to the determination of each next decimal place in the registers of the original number, the last positive difference is restored. After the tens decimal digit is found, the units decimal digit will remain in the original number registers.

The program "bin16ASCII5" (see Appendix, Program 2) converts a 16-bit binary integer number to an unpacked decimal number. The same algorithm is used.

3. Convert binary fraction to BCD

A binary fraction, by definition, is represented by the following expression:

0.A-1A-2 ... A-m = A-1*2-1 + A-2*2-2 + ... A-m*2-m

From this representation follows the transformation algorithm (Fig. 2), which contains m steps. At each step, the next binary digit is added to the binary-decimal result and the entire result is divided by 2.

Shown is a binary register that contains the original binary fraction and a BCD packed result register. For clarity, both registers also show the digit of units and the position of the point, which are not represented in any way in the microprocessor memory, but the position of which is always strictly specified. The number of cycles of the algorithm under consideration is equal to the number of bits of the binary fraction. The bit size of the binary decimal register is determined by the required calculation accuracy.

Adding this digit to a BCD number means that it must be placed in the units digit of the BCD number, from where, upon subsequent division by two, the digit A-i will move to the most significant digit of the most significant tetrad of the decimal fraction. When programming, we can think of the decimal units place as the C carry bit.

When dividing a packed binary number by two, just like when dividing a binary number, it is shifted to the right by one place. In this case, each tetrad, that is, each decimal digit, is divided by two. When dividing an even decimal digit in the corresponding place, the decimal digit is again obtained and no correction is required. When dividing by 2 an odd decimal digit, the remainder of 5 must be added to the lower decimal place, but in fact the binary shift adds the number 8 (the weight of the most significant tetrad digit) to the lower tetrad. Therefore, a correction of the result is required, which consists of subtracting the number 3 from the contents of those notebooks that, after shifting to the right, have the most significant digits set.

4. Converting Floating Point Numbers to BCD Numbers

The representation of floating point numbers is as follows:

where M is the binary mantissa of the number, P is the binary order of the number.

This representation is often used in the decimal number system to represent very large or very small numbers. The mantissa and exponent are signed integers. The mantissa sign is the sign of the entire number. The order shows the true position of the point instead of the one it occupies in the mantissa image. A binary floating point number differs from the decimal one we are used to in that the point is binary, that is, the order indicates the number of binary (rather than decimal) digits by which this point must be moved to the left or right.

A normalized representation of a floating point number is a representation where the mantissa is a proper fraction and its leading digit is different from zero. But for a binary number, the requirement that the leading digit is different from zero means that this digit is equal to 1. If the leading digit is precisely known, then it need not be stored in memory.

The concept of a mixed number system

Among number systems there is a class of so-called mixed number systems.

Definition 1

Mixed it's called this notation, in which numbers specified in a certain number system with base $P$ are represented using digits of another number system with base $Q$, where $Q

Moreover, in such a number system, in order to avoid discrepancies in the representation of each digit of the system with the base $P$, the same number of digits of the system with the base $Q$ is allocated, sufficient to represent any digit of the system with the base $P$.

An example of a mixed number system is the binary-decimal system.

Practical rationale for using the binary decimal number system

Since a person widely uses the decimal number system in his practice, and a computer typically operates with binary numbers and binary arithmetic, a compromise option was introduced into practice - binary decimal notation system, which is typically used where there is a need to frequently use the decimal input/output procedure (for example, electronic watches, calculators, etc.). In such devices, it is not always advisable to use universal microcode for converting binary numbers to decimal numbers and vice versa due to the small amount of program memory.

Note 1

In some types of computers, arithmetic logic units (ALUs) contain special decimal arithmetic units that perform operations on numbers represented in binary decimal code. This allows in some cases to significantly increase computer performance.

For example, an automated data processing system uses a large number of numbers but does not perform many calculations. In such a case, the operations of transferring numbers from one system to another would significantly exceed the time required to perform information processing operations. Microprocessors, on the other hand, use pure binary numbers, but they also understand commands to convert to binary decimal notation. The ALU of the AVR microcontroller (as well as other microprocessors) performs elementary arithmetic and logical operations on numbers represented in binary code, namely:

    reads the results of ADC conversion;

    in integer or floating-point format, processes measurement results.

However, the final result is displayed on the indicator in a decimal format that is convenient for human perception.

Principles of constructing a binary-decimal number system

When constructing a binary-decimal number system, $4$ binary digits are allocated to represent each decimal digit, since the maximum decimal digit $9$ is encoded as $10012$.

For example: $925_(10) = 1001 0010 0101_(2-10)$.

Picture 1.

In this notation, successive quadruples of binary digits represent the digits $9$, $2$ and $5$ of decimal notation, respectively.

To write a number in the binary-decimal number system, it must first be represented in the decimal system, and then each decimal digit included in the number must be represented in the binary system. At the same time, different numbers of binary digits are required to write different decimal digits in the binary number system. To avoid the use of any delimiters, when representing a decimal digit in binary, 4 binary digits are always written. The group of these four digits is called notebook.

Although BCD notation uses only the digits $0$ and $1$, it is different from the binary representation of a given number because the decimal equivalent of a binary number is several times larger than the decimal equivalent of a BCD number.

For example:

$1001 0010 0101_{(2)} = 2341_{(10)}$,

$1001 0010 0101_{(2)} = 925_{(2-10)}$.

This notation is quite often used as an intermediate step when converting a number from the decimal system to binary and vice versa. Since the number $10$ is not an exact power of the number $2$, not all $16$ tetrads are used (tetrads representing numbers from $A$ to $F$ are discarded, since these numbers are considered forbidden), but algorithms for arithmetic operations on multi-digit numbers in this case, more complex than in basic number systems. And yet, the binary decimal number system is used even at this level in many microcalculators and some computers.

To correct the results of arithmetic operations on numbers represented in binary decimal code, microprocessor technology uses commands that convert the results of operations into the binary decimal number system. The following rule is used: when a number greater than $9$ is obtained as a result of an operation (addition or subtraction) in a tetrad, the number $6$ is added to this tetrad.

For example: $75+18=$93.

$10001101\(8D)$

The forbidden digit $D$ appeared in the low notebook. Add $6$ to the low tetrad and get:

$10010011 \ (93)$

As you can see, despite the fact that the addition was carried out in the binary number system, the result of the operation was obtained in the binary-decimal number system.

Note 2

Bitwise balancing is often carried out based on binary decimal number system. The use of binary and binary-decimal number systems is most appropriate, since in this case the number of balancing cycles is the smallest among other number systems. Note that the use of binary code allows us to reduce the processing time of the compensating voltage by approximately $20\%$ compared to binary-decimal code.

Advantages of using the binary decimal number system

Converting numbers from the decimal system to the binary-decimal number system does not involve calculations and is easy to implement using simple electronic circuits, since a small number (4) of binary digits are converted. The reverse conversion occurs automatically in the computer using a special translation program.

The use of the binary-decimal number system in conjunction with one of the main number systems (binary) makes it possible to develop and create high-performance computers, since the use of a decimal arithmetic block in the ALU eliminates the need for programmed conversion of numbers from one number system to another when solving problems.

Since two binary decimal digits make up $1$ byte, which can be used to represent the values ​​of numbers from $0$ to $99$, and not from $0$ to $255$, as with an $8$-bit binary number, then using $1$ byte for By converting every two decimal digits, you can form BCD numbers with any desired number of decimal places.

An example of a mixed number system is binary decimal system . In the BCD number system, 4 binary digits are allocated for each decimal digit, since the maximum decimal digit 9 is encoded as 1001 2. For example,

925 10 = 1001 0010 0101 2-10 .

Here, successive quadruples (tetrads) of binary digits represent the digits 9, 2 and 5 of the decimal notation, respectively.

Although BCD notation uses only the digits 0 and 1, BCD notation is different from the binary representation of a given number. For example, the binary code 1001 0010 0101 corresponds to the decimal number 2341, not 925.

If P=Q l (l is a positive integer), the representation of any number in a mixed number system identically coincides with the image of this number in a number system with the base Q. Examples of such a mixed number system are binary-octal and binary-hexadecimal.

For example,

A2 16 = 1010 0010 2 = 1010 0010 2-16

REPRESENTATION OF NEGATIVE NUMBERS IN FIXED POINT FORMAT (DOTS)

In order to simplify arithmetic operations, computers use special binary codes to represent negative numbers: reciprocal and complement. Using these codes, it is simplified to determine the sign of the result of an operation during algebraic addition. The operation of subtraction (or algebraic addition) is reduced to the arithmetic addition of the operands, making it easier to develop signs of overflow of the bit grid. As a result, computer devices that perform arithmetic operations are simplified.

It is known that one of the ways to perform a subtraction operation is to replace the sign of the subtrahend with its opposite one and add it to the minuend:

A - B = A + (- B)

This replaces the operation of arithmetic subtraction with the operation of algebraic addition, which can be performed using binary adders.

For machine representation of negative numbers, codes are used direct, additional, reverse. A simplified definition of these codes can be given as follows. If the number A in ordinary binary code is direct binary code, depicted as

[A] pr = 0.an an-1 an-2.....a1 a0,

then the number -A in the same code is represented as

[-A]pr = 1.an an-1 an-2.....a1 a0,

and in reverse(inverse) code this number will look like:

[-A]rev = 1.an an-1 an-2.....a1 a0,

ai = 1 if ai = 0,

ai = 0, if ai = 1,

a i - digit i-that digit of a binary number. Consequently, when moving from a direct code to a reverse code, all digits of the Matisse number bits are inverted.

Then the number -A in additional the code is represented as

[-A]add = [-A]rev + 1

Thus, to obtain the complementary code of negative numbers, you must first invert the digital part of the original number, resulting in its reverse code, and then add one to the least significant digit of the digital part of the number.

The complementary code of a certain number is obtained by replacing it with a new number, complementary it to a number equal to the weight of the digit following the most significant digit of the bit grid used to represent the mantissa of the number in fixed-point format. Therefore, such a number code is called additional.

Let's imagine that we have only two digits to represent numbers in the decimal number system. Then the maximum number that can be depicted will be 99, and the weight of the third non-existent highest digit will be 10 2, i.e. 100. In this case, for the number 20, the complementary number will be 80, which complements 20 to 100 (100 - 20 = 80). Therefore, by definition, subtraction

can be replaced by addition:

Here the highest unit goes beyond the allocated bit grid, in which only the number 30 remains, i.e. The result of subtracting the number 20 from 50.

Now let's look at a similar example for numbers represented in 4-bit binary code. Let's find the additional number for 0010 2 = 210. We need to subtract 0010 from 0000, we get 1110, which is the additional code 2. The digit shown in square brackets does not actually exist. But since we have a 4-bit grid, it is basically impossible to perform such a subtraction, and even more so we are trying to get rid of subtraction. Therefore, the additional number code is obtained in the manner described earlier, i.e. first they get the reverse code of the number, and then add 1 to it. Having done all this with our number (2), it is not difficult to see that a similar answer will be obtained.

Let us emphasize that Two's complement and two's complement codes are used only to represent negative binary numbers in fixed-point form. Positive numbers in these codes do not change their image and are represented as in the direct code.

Thus, the digital digits of a negative number in direct code remain unchanged, and one is written in the sign part.

Let's look at simple examples.

Seven in direct code is represented as follows:

pr = 0.0001112

Number -7 in direct code:

[-7]pr = 1.0001112,

and in the reverse code it will look like

[-7]rev = 1.1110002,

those. ones are replaced by zeros, and zeros by ones. The same number in two's complement would be:

[-7]add = 1.1110012.

Let us consider once again how the subtraction procedure, using the representation of the subtrahend in two's complement code, is reduced to the addition procedure. Subtract the number 7 from 10: 10 - 7 = 3. If both operands are presented in direct code, then the subtraction procedure is performed as follows:

-1.000111

And if subtrahendable, i.e. -7, presented in two’s complement code, then the subtraction procedure is reduced to the addition procedure:

+ 1.111001

1 0.000011 = 310.

Nowadays, computers typically use two's complement code to represent negative numbers in fixed-point format.

The form of representation of numbers in digital machines is a set of rules that make it possible to establish a mutual correspondence between the recording of a number and its quantitative equivalent.

Machine (automatic) image of a number it is representation of a number in the bit grid of a digital machine. The symbol for a machine image of a number, for example, A, will be represented as [A].

Due to the limited length of machine words, the set of numbers that can be represented in a machine is finite. Comparisons between different forms of number representation in computers are usually made on the basis of an estimate range and accuracy of number representation.

In everyday practice, the most common form of representing numbers is as a sequence of digits separated by a comma into integer and fractional parts. Numbers represented in this form are called numbers with natural comma or numbers in natural form. In natural form, a number is written in its natural form, for example 12560 is an integer, 0.003572 is a proper fraction, 4.89760 is an improper fraction.

When representing numbers in this form, it is necessary for each number to indicate the position of its comma in the bit grid allocated to represent the number in the machine, which requires additional hardware costs of a fairly large amount. Therefore, two other forms of representation have become widespread in computers: with fixed and floating point (dot).

There is no need to indicate the position of the comma if the place of the comma in the bit grid of the machine is fixed in advance once and for all. This form of representing numbers is called representation with fixed comma (dot).

Since numbers can be positive and negative, the format (bit grid) of the machine image is divided into iconic part And number field. The number field contains the image of the number itself, which we will conventionally call mantissa numbers. To encode the sign of a number, the most significant digit of the bit grid reserved for the image of the binary number is used, and the remaining digits are allocated for the mantissa of the number. The position of the comma in the bit grid is strictly fixed, usually either to the right of the lowest digit of the mantissa, or to the left of the highest. In the first case, the number is represented as an integer, in the second - as a proper fraction. Nowadays, the vast majority of computers represent integers in fixed-point format.

The sign part contains information about the sign of the number. It is accepted that the sign positive number "+" represented by the symbol 0, and the sign is a negative number "-" represented by the symbol 1.

For example, in binary code, using a 6-bit grid, the number 7 in fixed-point form can be represented as:

where the digit to the left of the dot is the sign of the number, and the five digits to the right of the dot are the mantissa of the number in direct code. What is meant here is that comma is fixed to the right of the least significant digit, and the point in the image of the number in this case simply separates the sign bit from the mantissa of the number.

In the future, this type of representation of a number in machine form will often be used in examples. You can use another form of representing a number in machine form:

where the sign bit is separated by square brackets.

The number of digits in the bit grid allocated to represent the mantissa of a number determines the range and accuracy of the representation of a fixed-point number. The maximum binary number in absolute value is represented by ones in all digits, excluding the sign one, i.e. for integer

|A|max = (2 (n -1) - 1),

Where n- total length of the bit grid. In case of 16-bit grid

|A| max = (2 (16-1) - 1) = 32767 10,

those. The range of integer representation in this case will be from +3276710 to -3276710.

For the case when the comma is fixed to the right of the low-order digit of the mantissa, i.e. for integers, numbers whose modulus is greater than

(2(n-1) - 1) and less than one are not represented in fixed-point form. Numbers whose absolute value is less than one of the least significant digits of the bit grid are called in this case a machine zero. Negative zero is prohibited.

In some cases, when it is possible to operate only with moduli of numbers, the entire bit grid, including the most significant bit, is allocated to represent the number, which makes it possible to expand the range of representation of numbers.

Binary decimal number system

The binary decimal number system has become widespread in modern computers due to the ease of conversion to the decimal system and vice versa. It is used where the main attention is paid not to the simplicity of the technical construction of the machine, but to the convenience of the user. In this number system, all decimal digits are separately encoded by four binary digits and in this form are written sequentially one after another.

The binary decimal system is not economical from the point of view of implementing the technical construction of the machine (the required equipment increases by about 20%), but it is very convenient when preparing tasks and programming. In the binary decimal number system, the base of the number system is the number 10, but each decimal digit (0, 1, ..., 9) is represented, that is, encoded, by binary digits. Four binary digits are used to represent one decimal digit. Here, of course, there is redundancy, since 4 binary digits (or a binary tetrad) can represent not 10, but 16 numbers, but this is already a production cost for the sake of programming convenience. There are a number of binary-coded decimal systems for representing numbers, characterized in that certain combinations of zeros and ones within one tetrad are assigned certain values ​​of decimal digits.
Posted on ref.rf
In the most commonly used natural binary-coded decimal number system, the weights of binary digits within a tetrad are natural, that is, 8, 4, 2, 1 (Table 6).

Table 6

Binary decimal notation

For example, the decimal number 5673 in BCD notation is 01010110011100011.

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

1. To convert a binary number to a decimal number, it is necessary to write ᴇᴦο as a polynomial consisting of the products of the digits of the number and the corresponding power of 2, and calculate according to the rules of decimal arithmetic˸

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

Table 7.

Powers of number 2

n (degree)

Example. Convert the number to the decimal number system.

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

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

Table 8.

Powers of number 8

n (degree)
8 n

Binary decimal number system - concept and types. Classification and features of the category "Binary-decimal number system" 2015, 2017-2018.

This system has a base of S = 10, but each digit is represented by a four-bit binary number called a tetrad. Typically, this number system is used in computers when entering and outputting information. However, in some types of computers, the ALU contains special decimal arithmetic blocks that perform operations on numbers in binary decimal code. This allows in some cases to significantly increase computer performance.

For example, in an automated data processing system there are many numbers, but few calculations. In this case, operations associated with transferring numbers from one system to another would significantly exceed the time required to perform information processing operations.

Converting numbers from the decimal system to BCD is very simple and consists of replacing each digit with a binary tetrad.

Example.

Write the decimal number 572.38 (10) in the binary decimal number system.

The reverse translation is also simple: you need to divide the binary-decimal number into tetrads from the point to the left (for the integer part) and to the right (for the fractional part), add the required number of insignificant zeros, and then write each tetrad as a decimal digit.

Example.

Write the binary decimal number 10010.010101 (2-10) in the decimal number system.

Conversion of numbers from BCD to binary system is carried out according to the general rules described above.

2.3. Octal number system

In the octal number system, only eight digits are used, i.e. this number system has a base S = 8. In general, an octal number looks like this:

Where
.

The octal number system is not needed by a computer, unlike the binary system. It is convenient as a compact form of writing numbers and is used by programmers (for example, in program texts for a more concise and convenient way of writing binary codes of commands, addresses and operands). In the octal number system, the weight of each digit is a multiple of eight or one eighth, so an eight-bit binary number allows you to express decimal values ​​in the range 0-255, and an octal number covers the range 0-99999999 (for binary this is 27 digits).

Since 8=2 3, each octal character can be represented as a three-bit binary number. To convert a number from the binary number system to the octal number system, you need to divide this number to the left (for the integer part) and to the right (for the fractional part) of the point (comma) into groups of three digits (triads) and represent each group with a number in the octal number system. The extreme incomplete triads are supplemented with the required number of insignificant zeros.

Example.

Write the binary number 10101011111101 (2) in the octal number system.

Example.

Write the binary number 1011.0101 (2) in the octal number system.

Conversion from octal to binary is carried out by representing each digit of an octal number as a three-digit binary number (triad).

2.4. Hexadecimal number system

This number system has a base of S = 16. In general, a hexadecimal number looks like this:

Where
.

The hexadecimal number system makes it possible to write multi-bit binary numbers even more briefly and, in addition, to shorten the notation of a 4-bit binary number, i.e. nibble, since 16=2 4 . The hexadecimal system is also used in program texts for a more concise and convenient recording of binary numbers.

To convert a number from the binary number system to hexadecimal, you need to divide this number to the left and right of the point into tetrads and represent each tetrad with a digit in the hexadecimal number system.

Example.

Write the binary number 10101011111101 (2) in hexadecimal.

Example.

Write the binary number 11101.01111 (2) in hexadecimal.

To convert a number from the hexadecimal number system to the binary number system, it is necessary, on the contrary, to replace each digit of this number with a tetrad.

In conclusion, it should be noted that the transfer of arbitrary numbers from one number system to another can be carried out according to the general rules described in the section “Binary number system”. However, in practice, translations of numbers from the decimal system to the considered number systems and vice versa are carried out through the binary number system.

Also, remember that hexadecimal and octal numbers are only a way of representing the large binary numbers that the processor actually operates on. In this case, the hexadecimal system is preferable, since in modern computers processors manipulate words of length 4, 8, 16, 32 or 64 bits, i.e. the length of words is a multiple of 4. In the octal number system, words that are multiples of 3 bits are preferred, for example, words with a length of 12 bits (as in the DEC PDP-8).