Accounting calculations using the binary decimal number system. Representing numbers in binary code

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 of 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 computers. Later, new electrical control devices were created - electronic circuits, consisting of a set of semiconductor elements. Such electronic circuits, which convert signals of only two fixed voltages of 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 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 to the 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 the truth values ​​of the input signals (operands) along with the truth value of the output signal (result of the operation) for each of these combinations.

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 (DOTT) FORMAT

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 following, 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 the case of a 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)
8n

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, conversions 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 PDP-8 from DEC).

In computer science courses, regardless of school or university, a special place is given to such a concept as number systems. As a rule, several lessons or practical exercises are allocated for it. The main goal is not only to master the basic concepts of the topic, to study the types of number systems, but also to get acquainted with binary, octal and hexadecimal arithmetic.

What does it mean?

Let's start by defining the basic concept. As the textbook "Informatics" notes, a number system is a record of numbers that uses a special alphabet or a specific set of numbers.

Depending on whether the value of a digit changes depending on its position in the number, there are two: positional and non-positional number systems.

In positional systems, the meaning of a digit changes along with its position in the number. So, if we take the number 234, then the number 4 in it means units, but if we consider the number 243, then it will already mean tens, not units.

In non-positional systems, the meaning of a digit is static, regardless of its position in the number. The most striking example is the stick system, where each unit is indicated by a dash. It doesn’t matter where you place the stick, the value of the number will only change by one.

Non-positional systems

Non-positional number systems include:

  1. A unit system that is considered one of the first. It used sticks instead of numbers. The more there were, the greater the value of the number. You can find an example of numbers written in this way in films where we are talking about people lost at sea, prisoners who mark each day with the help of notches on a stone or tree.
  2. Roman, in which Latin letters were used instead of numbers. Using them, you can write any number. Moreover, its value was determined using the sum and difference of the digits that made up the number. If there was a smaller number to the left of the digit, then the left digit was subtracted from the right, and if the digit to the right was less than or equal to the digit on the left, then their values ​​were summed. For example, the number 11 was written as XI, and 9 - IX.
  3. Alphabetical, in which numbers were designated using the alphabet of a particular language. One of them is considered to be the Slavic system, in which a number of letters had not only phonetic, but also numerical meaning.
  4. in which only two notations were used for writing - wedges and arrows.
  5. Egypt also used special symbols to represent numbers. When writing a number, each symbol could be used no more than nine times.

Position systems

Much attention is paid in computer science to positional number systems. These include the following:

  • binary;
  • octal;
  • decimal;
  • hexadecimal;
  • sexagesimal, used when counting time (for example, there are 60 seconds in a minute, 60 minutes in an hour).

Each of them has its own alphabet for writing, rules for translation and performing arithmetic operations.

Decimal system

This system is the most familiar to us. It uses the numbers 0 to 9 to write numbers. They are also called Arabic. Depending on the position of the digit in the number, it can represent different digits - units, tens, hundreds, thousands or millions. We use it everywhere, we know the basic rules by which arithmetic operations on numbers are performed.

Binary system

One of the main number systems in computer science is binary. Its simplicity allows the computer to perform cumbersome calculations several times faster than in the decimal system.

To write numbers, only two digits are used - 0 and 1. Moreover, depending on the position of 0 or 1 in the number, its value will change.

Initially, it was with the help of computers that they received all the necessary information. In this case, one meant the presence of a signal transmitted using voltage, and zero meant its absence.

Octal system

Another well-known computer number system, which uses numbers from 0 to 7. It was used mainly in those areas of knowledge that are associated with digital devices. But recently it has been used much less frequently, since it has been replaced by the hexadecimal number system.

Binary decimal system

Representing large numbers in binary is a rather complicated process for humans. To simplify it, it was developed. It is usually used in electronic watches and calculators. In this system, not the entire number is converted from the decimal system to binary, but each digit is converted to its corresponding set of zeros and ones in the binary system. The conversion from binary to decimal occurs in a similar way. Each digit, represented as a four-digit set of zeros and ones, is converted into a decimal number system digit. In principle, there is nothing complicated.

To work with numbers in this case, a table of number systems will be useful, which will indicate the correspondence between the numbers and their binary code.

Hexadecimal system

Recently, the hexadecimal number system has become increasingly popular in programming and computer science. It uses not only numbers from 0 to 9, but also a number of Latin letters - A, B, C, D, E, F.

At the same time, each of the letters has its own meaning, so A=10, B=11, C=12 and so on. Each number is represented as a set of four characters: 001F.

Converting numbers: from decimal to binary

Translation in number systems occurs according to certain rules. The most common conversion is from binary to decimal system and vice versa.

In order to convert a number from the decimal system to the binary system, it is necessary to sequentially divide it by the base of the number system, that is, the number two. In this case, the remainder of each division must be recorded. This will happen until the remainder of the division is less than or equal to one. It is best to carry out calculations in a column. The resulting division remainders are then written to the line in reverse order.

For example, let's convert the number 9 to binary:

We divide 9, since the number is not divisible by a whole, then we take the number 8, the remainder will be 9 - 1 = 1.

After dividing 8 by 2, we get 4. Divide it again, since the number is divisible by an integer - we get a remainder of 4 - 4 = 0.

We carry out the same operation with 2. The remainder is 0.

As a result of division we get 1.

Regardless of the final number system, the conversion of numbers from decimal to any other will occur according to the principle of dividing the number by the base of the positional system.

Converting numbers: from binary to decimal

It is quite easy to convert numbers into the decimal number system from binary. To do this, it is enough to know the rules for raising numbers to powers. In this case, to the power of two.

The translation algorithm is as follows: each digit from the code of a binary number must be multiplied by two, and the first two will be to the power of m-1, the second - m-2 and so on, where m is the number of digits in the code. Then add the results of the addition to obtain an integer.

For schoolchildren, this algorithm can be explained more simply:

To begin with, we take and write down each digit multiplied by two, then put the power of two from the end, starting from zero. Then we add up the resulting number.

As an example, we will analyze the number 1001 obtained earlier, converting it to the decimal system, and at the same time check the correctness of our calculations.

It will look like this:

1*2 3 + 0*2 2 +0*2 1 +1*2 0 = 8+0+0+1 =9.

When studying this topic, it is convenient to use a table with powers of two. This will significantly reduce the amount of time required to carry out calculations.

Other translation options

In some cases, translation can be carried out between binary and octal number systems, binary and hexadecimal. In this case, you can use special tables or launch a calculator application on your computer by selecting the “Programmer” option in the View tab.

Arithmetic operations

Regardless of the form in which the number is presented, it can be used to carry out calculations that are familiar to us. This can be division and multiplication, subtraction and addition in the number system you have chosen. Of course, each of them has its own rules.

So for the binary system, its own tables have been developed for each of the operations. The same tables are used in other positional systems.

There is no need to memorize them - just print them out and have them on hand. You can also use a calculator on your PC.

One of the most important topics in computer science is the number system. Knowledge of this topic, understanding of algorithms for converting numbers from one system to another is the key to the fact that you will be able to understand more complex topics, such as algorithmization and programming, and will be able to write your first program yourself.