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 ten, but each of the 10 decimal digits (0, 1, ..., 9) is represented using binary digits, that is, encoded in binary digits. Four binary digits are used to represent one decimal digit. There is, of course, redundancy here, since four 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 1 .

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 3.1).

Table 3.1. Table of binary codes of decimal and hexadecimal digits

Number Code Number Code
A
B
C
D
E
F

For example, the decimal number 9703 in BCD looks like this: 1001011100000011.

Question 18. os. Logical principles of computer operation. Logic algebra operations

The algebra of logic involves many logical operations. However, three of them deserve special attention, because... with their help, you can describe all the others, and, therefore, use less variety of devices when designing circuits. Such operations are conjunction(AND), disjunction(OR) and negation(NOT). Often the conjunction is denoted & , disjunction - || , and the negation is a bar over the variable indicating the statement.

With a conjunction, the truth of a complex expression arises only if all the simple expressions that make up the complex are true. In all other cases, the complex expression will be false.

With disjunction, the truth of a complex expression occurs when at least one simple expression included in it is true, or two at once. It happens that a complex expression consists of more than two simple ones. In this case, it is enough for one simple to be true and then the whole statement will be true.

Negation is a unary operation, because it is performed in relation to one simple expression or in relation to the result of a complex one. As a result of negation, a new statement is obtained that is opposite to the original one.

Question 19. Basic rules of algebra logic

The usual notation for these laws in formal logic is:

Question 20. Truth table

Truth tables

It is convenient to describe logical operations by the so-called truth tables, which reflect the results of calculations of complex statements for different values ​​of the original simple statements. Simple statements are denoted by variables (for example, A and B).

21 Question. Logical elements. Their names and designations on the diagram

How can we use the knowledge we have gained from the field of mathematical logic to design electronic devices? We know that O and 1 in logic are not just numbers, but a designation of states of some object in our world, conventionally called “false” and “true”. Such an object, which has two fixed states, can be an electric current. Devices that detect two stable states are called bistable(e.g. switch, relay). If you remember the first computers were relay. Later, new electrical control devices were created - electronic circuits, consisting of a set of semiconductor elements. Such electronic circuits that convert signals of only two fixed voltages electric current(bistable), began to be called logical elements.

Computer logic element- this is part of an electronic logic circuit that implements an elementary logical function.

The logical elements of computers are electronic circuits AND, OR, NOT, NAND, NOR and others (also called valves), and also trigger.

Using these circuits, you can implement any logical function that describes the operation of computer devices. Typically, valves have two to eight inputs and one or two outputs.

To represent the two logic states “1” and “0” in the gates, their corresponding input and output signals have one of two set voltage levels. For example, +5 volts and 0 volts.

A high level usually corresponds to the value “true” (“1”), and a low level usually corresponds to a value “false” (“0”).

Each logical element has its own symbol, which expresses its logical function, but does not indicate what kind of electronic circuit is implemented in it. This makes it easier to write and understand complex logic circuits.

The operation of logical elements is described using truth tables.

Truth table is a tabular representation of a logic circuit (operation) that lists all possible combinations of truth values input signals(operands) along with the truth value of the output signal (result of the operation) for each of these combinations.

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, in automated system Data processing uses a large number of numbers, but there are few 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 over 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)$.

Figure 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 binary given number, since the decimal equivalent of a binary number is several times greater than the decimal equivalent of a binary number decimal 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 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 together with one of the main number systems (binary) allows you 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 when using an $8$ binary number, then using $1$ byte for By converting every two decimal digits, you can form BCD numbers with any required number of decimal places.


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 focus is not on the simplicity of the technical construction of the machine, but on the user experience. 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. 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, you need to write it down as a polynomial, consisting of the products of the digits of the 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 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, you need to write it down as 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 8.

Powers of number 8

n (degree)
8 n

Example. Convert the number 75013 8 to the decimal number system.

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.

IN this section Programs for converting binary numbers to BCD are considered.

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 ones 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, when subsequently divided by two digit A-i will move to the most significant digit of the most significant decimal tetrad. 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 of 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 (not 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.

Binary-coded decimal number system (D-codes)

Direct representation of decimal numbers leads to the need for binary coding of decimal digits. Devices that perform arithmetic conversions with decimal numbers are given the special term "decimal arithmetic". Such devices should be as similar as possible to conventional binary devices.

Decimal arithmetic is included in the hardware of high-performance systems to eliminate conversion of source data to binary and results to decimal.

The binary coded decimal system is combined system number system, which has the advantages of binary and the convenience of the decimal system.

D-code is a binary-coded representation of a decimal number, in which each decimal digit is represented by a tetrad of binary characters.

Number of different binary tetrads N= 2 4 = 16. Only ten of them are used to encode binary digits. The presence of redundant combinations allows you to have different D-codes. In computers, the most widely used coding systems are 8421 - D 1 , 2421 - D 2 , (8421+3) - D 4. The resulting redundancy leads to multiple encodings of decimal digits, from which the optimal one must be selected.

Code 8421 (Table 2.4) is called code with natural weights, where the numbers 8,4,2,1 are the weights of the binary digits of the tetrads. Any decimal digit in this code is represented by its equivalent in the binary number system. This code has found its greatest use in encoding decimal numbers in input/output devices and in building decimal arithmetic operating devices.

Features of the codes D 2 and D 4 (8421+3) or a code with an excess of 3 is that the coding of any decimal digit and its complementary digit up to 9 is carried out by mutually complementary tetrads. This feature provides an easy way to obtain 9's complement by inverting the binary digits of the tetrad. Such codes are convenient to use to organize the subtraction operation when constructing decimal adders.

Table 2.4

Examples of encoding decimal digits in tetrads

Decimal digit

Equivalents in D-codes

D 1 (8421)

D 2 (2421)

D 4 (8421+3)

Here is an example of coding the decimal number A = 8371 in a binary-coded decimal number system:

D 1: A = 1000 0011 0111 0001 (2/10) ;

D 2: A = 1110 0011 1101 0001 (2/10) ;

D 4: A = 1011 0110 1010 0100 (2/10).

Optimal encoding is determined by six requirements that a decimal code must satisfy.

1. Unambiguity. Each decimal digit must correspond to a specific, different binary code.

Failure to comply with this requirement leads to ambiguous results.

2. Orderliness. Large decimal digits should correspond to large decimal code tetrads and, conversely, smaller decimal digits should correspond to smaller tetrads.

Fulfillment of this requirement is necessary to organize a quantitative comparison of numbers in decimal places.

3. Parity. Even digits must correspond to even tetrads, and odd digits must correspond to odd tetrads. Compliance can be marked in any way.

Fulfilling this requirement is necessary to round the result.

4. Complementarity. If x1 and x2 are two digits for which x1 + x2 = 9 and the digit x1 is associated with a tetrad, then the digit x2, if the complementarity requirement is satisfied, must be associated with a tetrad obtained by inverting the binary digits of the code of the digit x1.

The complementarity requirement is necessary to simplify the implementation of complementary and reciprocal codes for decimal numbers.

5. Significance. There must be four positive integers: p3, p2, p1, p0, called weights, with which you can determine the decimal digit x from the value of the binary tetrad associated with x, using the formula

Fulfilling this requirement facilitates decoding.

6. Continuity. A continuous sequence of changes in the meaning of the digits must correspond to a continuous sequence of changes in the meaning of the tetrads.

No decimal code satisfies all six of these requirements at the same time.

The most widespread in VT is the direct substitution code with a digit weight of 8421. This code is the most visual and convenient, since, in accordance with the name of the code, the decimal digit in it is the corresponding value of the binary code. However, code 8421 does not satisfy the complementarity requirement, so actions in this code that change the sign of a decimal number involve inverting digits or taking the complement, that is, they require additional corrections and/or time.

The advantages of the binary-coded decimal number system relative to the binary number system are:

  • · no need to convert source data and results from one number system to another;
  • · convenience of monitoring intermediate results by displaying them for internal monitoring;
  • · wider possibilities for automatic control due to the availability of D-codes of redundant combinations.

D-codes are used to solve economic problems that are characterized by a large volume of initial data, comparative simplicity and a small amount of transformations performed on them and a large number of calculation results. This system is widely used in calculators and personal microcomputers.


Close