Representing numbers on a computer. Representation of integers and real numbers in computer memory

In computer technology, real numbers (as opposed to integers) are numbers that have a fractional part.

When writing them Instead of a comma, it is customary to write a period. So, for example, the number 5 is an integer, and the numbers 5.1 and 5.0 are real.

For the convenience of displaying numbers that take values ​​from a fairly wide range (that is, both very small and very large), the form of writing numbers with base order of the number system. For example, the decimal number 1.25 can be represented in this form as follows:

1.25*10 0 = 0.125*10 1 = 0.0125*10 2 = ... ,
or like this:
12.5*10 -1 = 125.0*10 -2 = 1250.0*10 -3 = ... .

If the “floating” point is located in the mantissa before the first significant digit, then with a fixed number of digits allocated for the mantissa, the maximum number of significant digits of the number is recorded, that is, the maximum accuracy of the number’s representation in the machine. From this it follows:

This representation of real numbers, which is most beneficial for a computer, is called normalized.

The mantissa and the order of a q-ary number are usually written in the system with the base q, and the base itself is written in the decimal system.

Examples of normalized representation:

Decimal system Binary system

753.15 = 0.75315*10 3 ; -101.01 = -0.10101*2 11 (order 11 2 = 3 10)

0.000034 = -0.34*10 -4 ; -0.000011 = 0.11*2 -100 (order -100 2 = -410)

Real numbers are written differently in different types of computers. In this case, the computer usually gives the programmer the opportunity to choose from several number formats the most suitable for a particular task - using four, six, eight or ten bytes.

As an example, here are the characteristics of the real number formats used by IBM-compatible personal computers:

Real number formats Size in bytes Approximate range of absolute values Number of significant decimal digits
Single 4 10 -45 ... 10 38 7 or 8
Real 6 10 -39 ... 10 38 11 or 12
Double 8 10 -324 ... 10 308 15 or 16
Advanced 10 10 -4932 ... 10 4932 19 or 20

From this table it can be seen that the floating-point form of representation allows you to write numbers with high precision and from a very wide range.

When storing floating point numbers, they are allocated digits for mantissa, exponent, number sign and exponent sign:

Let us show with examples how some numbers are written in a normalized form in a four-byte format with seven bits to record the order.

1. Number 6.25 10 = 110.01 2 = 0.11001

  • 2 11:

2. Number -0.125 10 = -0.0012 = -0.1*2 -10 (negative order is written in two's complement):

Anyone who has ever thought about becoming an “IT specialist” or system administrator, and simply linking one’s fate with knowledge of how numbers are represented is absolutely necessary. After all, this is what low-level programming languages ​​such as Assembler are based on. Therefore, today we will look at the representation of numbers in a computer and their placement in memory cells.

Notation

If you're reading this article, you probably already know this, but it's worth repeating. All data in personal computer stored in binary This means that any number must be represented in the appropriate form, that is, consisting of zeros and ones.

To convert the decimal numbers familiar to us to a form understandable by a computer, you need to use the algorithm described below. There are also specialized calculators.

So, in order to convert a number to the binary number system, we need to take the value we have chosen and divide it by 2. After this, we will get the result and the remainder (0 or 1). We divide the result again by 2 and remember the remainder. This procedure must be repeated until the end result is also 0 or 1. Then we write down the final value and remainders in the reverse order as we received them.

This is exactly how numbers are represented in a computer. Any number is written in binary form and then occupies a memory cell.

Memory

As you should already know, the smallest unit of information is 1 bit. As we have already found out, numbers are represented in a computer in binary format. Thus, each bit of memory will be occupied by one value - 1 or 0.

Cells are used for storage. Each such unit contains up to 8 bits of information. Therefore, we can conclude that the minimum value in each memory segment can be 1 byte or be an eight-digit binary number.

Whole

Finally, we come to the direct placement of data on the computer. As already mentioned, the processor first converts information into binary format, and only then places it in memory.

Let's start from the very simple option, which is the representation of integers in a computer. PC memory allocates a ridiculously small number of cells for this process - only one. Thus, a maximum of one slot can contain values ​​from 0 to 11111111. Let's convert the maximum number into the notation form we are familiar with.
X = 1 × 2 7 + 1 × 2 6 + 1 × 2 5 + 1 × 2 4 + 1 × 2 3 + 1 × 2 2 + 1 × 2 1 + 1 × 2 0 = 1 × 2 8 - 1 = 255 .

Now we see that one memory cell can contain a value from 0 to 255. However, this applies exclusively to non-negative integers. If the computer needs to write a negative value, everything will work a little differently.

Negative numbers

Now let's look at how numbers are represented in a computer if they are negative. To accommodate a value that is less than zero, two memory cells, or 16 bits of information, are allocated. In this case, 15 goes under the number itself, and the first (leftmost) bit goes under the corresponding sign.

If the number is negative, then “1” is written, if positive, then “0”. To make it easier to remember, we can draw the following analogy: if there is a sign, then we put 1, if it is not there, then nothing (0).

The remaining 15 bits of information are allocated to the number. Similar to the previous case, they can accommodate a maximum of fifteen units. It is worth noting that the recording of negative and positive numbers is significantly different from each other.

In order to place a value greater than or equal to zero in 2 memory cells, the so-called direct code is used. This operation is performed in the same way as described, and the maximum A = 32766, if used. I would immediately like to note that in this case “0” refers to positive ones.

Examples

Representing integers in computer memory is not such a difficult task. Although it gets a little more complicated if we are talking about a negative value. To write a number that is less than zero, two's complement code is used.

To obtain it, the machine performs a number of auxiliary operations.

  1. First the module is written negative number in binary numbers. That is, the computer remembers a similar, but positive value.
  2. Each bit of memory is then inverted. To do this, all ones are replaced by zeros and vice versa.
  3. Add "1" to the result. This will be the additional code.

Let's give a clear example. Let us have a number X = - 131. First we get its modulus |X|= 131. Then we convert it to the binary system and write it in 16 cells. We get X = 0000000010000011. After inversion, X = 1111111101111100. We add “1” to it and get the return code X=1111111101111101. To write to a 16-bit memory cell, the minimum number is X = - (2 15) = - 32767.

Long integers

As you can see, representing real numbers in a computer is not that difficult. However, the considered range may not be sufficient for most operations. Therefore, in order to accommodate large numbers, the computer allocates 4 cells, or 32 bits, from memory.

The recording process is absolutely no different from the one presented above. So we'll just give the range of numbers that can be stored in this type.

X max =2,147,483,647.

X min = - 2 147 483 648.

In most cases, these values ​​are sufficient to record and perform operations with data.

Representing real numbers in a computer has its advantages and disadvantages. On the one hand, this technique makes it easier to perform operations between integer values, which significantly speeds up the processor. On the other hand, this range is not enough to solve most problems in economics, physics, arithmetic and other sciences. Therefore, now we will consider another technique for super-magnitudes.

floating point

This is the last thing you need to know about representing numbers in a computer. Since when writing fractions there is a problem of determining the position of the decimal point in them, scientific notation is used to place such digits in the computer.

Any number can be represented in the following form X = m * p n. Where m is the mantissa of the number, p is the base of the number system and n is the exponent of the number.

To standardize the recording of floating point numbers, the following condition is used, according to which the modulus of the mantissa must be greater than or equal to 1/n and less than 1.

Let us be given the number 666.66. Let's put it in exponential form. It turns out X = 0.66666 * 10 3. P = 10 and n = 3.

Floating point values ​​are typically allocated 4 or 8 bytes (32 or 64 bits). In the first case it is called a regular precision number, and in the second case it is called a double precision number.

Of the 4 bytes allocated for storing digits, 1 (8 bits) is allocated for data about the order and its sign, and 3 bytes (24 bits) are used to store the mantissa and its sign according to the same principles as for integer values. Knowing this, we can carry out simple calculations.

Maximum value n = 1111111 2 = 127 10. Based on it, we can get the maximum size of a number that can be stored in computer memory. X=2 127 . Now we can calculate the maximum possible mantissa. It will be equal to 2 23 - 1 ≥ 2 23 = 2 (10 × 2.3) ≥ 1000 2.3 = 10 (3 × 2.3) ≥ 10 7. As a result, we received an approximate value.

If we now combine both calculations, we get a value that can be written without loss into 4 bytes of memory. It will be equal to X = 1.701411 * 10 38. The remaining numbers were discarded, since this is precisely the accuracy that allows us to have this method records.

Double precision

Since all the calculations were described and explained in the previous paragraph, here we will tell everything very briefly. For double precision numbers, there are usually 11 bits for the exponent and its sign, as well as 53 bits for the mantissa.

P = 1111111111 2 = 1023 10.

M = 2 52 -1 = 2 (10*5.2) = 1000 5.2 = 10 15.6. We round up and get the maximum number X = 2 1023 accurate to the nearest “m”.

We hope that the information we have provided about the representation of integer and real numbers in a computer will be useful to you in your studies and will be at least a little clearer than what is usually written in textbooks.

The maximum value of a non-negative integer is achieved when all cells contain ones. For an n-bit representation it will be equal to

non-negative integers. The minimum number corresponds to the eight zeros stored in the eight bits of the memory cell and is equal to zero. The maximum number corresponds to eight units and is equal to

A = 1 × 2 7 + 1 × 2 6 + 1 × 2 5 + 1 × 2 4 + 1 × 2 3 + 1 × 2 2 + 1 × 2 1 + 1 × 2 0 = 1 × 2 8 - 1 = 255 10.

Range of change non-negative integers numbers: from 0 to 255.

For storage signed integers two memory cells (16 bits) are allocated, and the most significant (left) bit is allocated to the sign of the number (if the number is positive, then 0 is written to the sign bit, if the number is negative - 1).

The representation of positive numbers in a computer using sign-magnitude format is called direct code numbers. For example, the number 2002 10 = 11111010010 2 would be represented in 16-bit format as follows:

0 0 0 0 0 1 1 1 1 1 0 1 0 0 1 0

The maximum positive number (allowing for one digit per sign) for signed integers in n-bit representation is:

Used to represent negative numbers additional code. Additional code allows you to replace the arithmetic subtraction operation with an addition operation, which significantly simplifies the processor's operation and increases its performance.

The complement code of a negative number A stored in n cells is 2 n - |A|.

Two's complement represents the addition of the modulus of a negative number A to 0, since in n-bit computer arithmetic:

2 n - |A| + |A| = 0,

since in computer n-bit arithmetic 2 n = 0. Indeed, the binary representation of such a number consists of one one and n zeros, and only n low-order digits, that is, n zeros, can fit into an n-bit cell.

To obtain the complement code of a negative number, you can use a fairly simple algorithm:

1. Write the module of the number in direct code in n binary digits.

2. Get return code numbers, for this purpose, invert the values ​​of all bits (replace all ones with zeros and replace all zeros with ones).

3. Add one to the resulting reverse code.

Let's write the additional code of the negative number -2002 for the 16-bit computer representation:


When an n-bit representation of a negative number A in two's complement code is used, the most significant bit is allocated to store the sign of the number (one). The remaining digits are written as positive numbers.

For a number to be positive, the following condition must be true:

|A| £ 2 n-1 .

Therefore, the maximum value of the modulus of the number A in the m-digit representation is equal to:

Then the minimum negative number is:

Let's define the range of numbers that can be stored in RAM in format long signed integers(four memory cells are allocated for storing such numbers - 32 bits).

The maximum positive integer (taking into account the allocation of one digit per sign) is equal to:

A = 2 31 - 1 = 2 147 483 647 10.

The minimum negative integer is:

A = -2 31 = - 2 147 483 648 10.

Advantages of representing numbers in format with fixed point are the simplicity and clarity of the representation of numbers, as well as the simplicity of the algorithms for implementing arithmetic operations.

The disadvantage of representing numbers in format with fixed point is a small range of representation of quantities, insufficient for solving mathematical, physical, economic and other problems in which both very small and very large numbers are used.

Representation of numbers in floating point format. Real numbers are stored and processed in a computer in the format floating point. In this case, the position of the decimal point in the number may change.

Number format floating point is based on exponential notation, in which any number can be represented. So the number A can be represented as:

A = m × q n 2.3

where m is the mantissa of the number;
q - base of the number system;
n - number order.

For uniform presentation of numbers floating point a normalized form is used in which the mantissa meets the condition:

1/n £ |m|

This means that the mantissa must be a proper fraction and have a non-zero digit after the decimal point.

Let's convert the decimal number 555.55, written in natural form, into exponential form with a normalized mantissa:

555.55 = 0.55555 × 10 3.

Here the normalized mantissa: m = 0.55555, order: n = 3.

A number in floating point format takes up 4 ( common precision number) or 8 bytes ( double precision number). When writing a floating point number, bits are allocated to store the mantissa sign, exponent sign, exponent and mantissa.

The range of numbers is determined by the number of bits allocated to store the order of the number, and the precision (the number of significant digits) is determined by the number of bits allocated to store the mantissa.

Let's determine the maximum number and its accuracy for the format ordinary precision numbers, if 8 bits are allocated to store the order and its sign, and 24 bits are allocated to store the mantissa and its sign:

0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
sign and order sign and mantissa

The maximum value of the order of the number will be 1111111 2 = 127 10, and therefore the maximum value of the number will be:

2 127 = 1.7014118346046923173168730371588 × 10 38.

The maximum value of a positive mantissa is:

2 23 - 1 » 2 23 = 2 (10 × 2.3) » 1000 2.3 = 10 (3 × 2.3) » 10 7.

Thus the maximum value ordinary precision numbers taking into account the possible accuracy of calculations, it will be 1.701411 × 10 38 (the number of significant digits of a decimal number in this case is limited to 7 digits).

Quests

1.26. Fill out the table by writing negative decimal numbers in forward, backward and complement codes in 16-bit notation:

1.27. Define view range signed integers(2 bytes of memory are allocated) in fixed point format.

1.28. Determine the maximum number and its precision for the format double precision numbers, if 11 bits are allocated to store the order and its sign, and 53 bits are allocated to store the mantissa and its sign.

Numerical data is processed in a computer in binary system Reckoning. Numbers are stored in computer memory in binary code, that is, as a sequence of zeros and ones, and can be represented in fixed or floating point format.

Integers are stored in memory in fixed-point format. With this format for representing numbers, a memory register consisting of eight memory cells (8 bits) is allocated for storing non-negative integer numbers. Each digit of a memory cell always corresponds to the same digit of the number, and the comma is located to the right after the least significant digit and outside the digit grid. For example, the number 110011012 would be stored in a memory register as follows:

Table 4

The maximum value of a non-negative integer number that can be stored in a register in fixed-point format can be determined from the formula: 2n – 1, where n is the number of digits of the number. The maximum number will be equal to 28 - 1 = 25510 = 111111112 and the minimum 010 = 000000002. Thus, the range of changes in non-negative integers will be from 0 to 25510.

Unlike the decimal system in the binary number system in computer representation binary number There are no symbols to indicate the sign of a number: positive (+) or negative (-), so to represent signed integers in the binary system, two number representation formats are used: signed number format and two's complement format. In the first case, two memory registers (16 bits) are allocated for storing signed integers, and the most significant digit (leftmost) is used as the sign of the number: if the number is positive, then 0 is written to the sign bit, if the number is negative, then 1. For example , the number 53610 = 00000010000110002 will be represented in the memory registers in the following form:

Table 5

and a negative number -53610 = 10000010000110002 in the form:

Table 6

The maximum positive number or minimum negative number in signed number value format (taking into account the representation of one digit per sign) is 2n-1 – 1 = 216-1 – 1 = 215 – 1 = 3276710 = 1111111111111112 and the range of numbers will be from - 3276710 to 32767.

Most often, to represent signed integers in the binary system, the two's complement code format is used, which allows you to replace the arithmetic operation of subtraction in a computer with an addition operation, which significantly simplifies the structure of the microprocessor and increases its performance.

To represent negative integers in this format, one's two's complement code is used, which is the modulus of a negative number's complement to zero. Converting a negative integer to two's complement is carried out using the following operations:


1) write the module of the number in direct code in n (n = 16) binary digits;

2) get the reverse code of the number (invert all digits of the number, i.e. replace all ones with zeros, and zeros with ones);

3) add one to the least significant digit to the resulting reverse code.

For example, for the number -53610 in this format, the modulus will be 00000010000110002, the reciprocal code will be 1111110111100111, and the additional code will be 1111110111101000.

It must be remembered that the complement of a positive number is the number itself.

To store signed integers other than the 16-bit computer representation when used two memory registers(this number format is also called the short signed integer format), the medium and long signed integer formats are used. To represent numbers in the mid number format, four registers are used (4 x 8 = 32 bits), and to represent numbers in the long number format, eight registers are used (8 x 8 = 64 bits). The ranges of values ​​for the medium and long number formats will be respectively: -(231 – 1) ... + 231 – 1 and -(263-1) ... + 263 – 1.

Computer representation of numbers in fixed point format has its advantages and disadvantages. TO benefits include the simplicity of representing numbers and algorithms for implementing arithmetic operations; the disadvantages are the finite range of representation of numbers, which may be insufficient for solving many problems of a practical nature (mathematical, economic, physical, etc.).

Real numbers (finite and infinite decimals) are processed and stored in a computer in floating point format. With this number representation format, the position of the decimal point in the entry may change. Any real number K in floating point format can be represented as:

where A is the mantissa of the number; h – base of the number system; p – number order.

Expression (2.7) for the decimal number system will take the form:

for binary -

for octal -

for hexadecimal -

This form of number representation is also called normal . With a change in order, the comma in the number shifts, that is, it seems to float to the left or to the right. Therefore, the normal form of representing numbers is called floating point form. The decimal number 15.5, for example, in floating point format can be represented as: 0.155 102; 1.55 101; 15.5 100; 155.0 10-1; 1550.0 10-2 etc. This form of floating point decimal 15.5 is not used when writing computer programs and entering them into the computer (computer input devices perceive only linear data recording). Based on this, expression (2.7) for representing decimal numbers and entering them into the computer is converted to the form

where P is the order of number,

i.e., instead of the base of the number system 10, they write the letter E, instead of a comma, a dot, and the multiplication sign is not placed. Thus, the number 15.5 in floating point and linear format (computer representation) will be written as: 0.155E2; 1.55E1; 15.5E0; 155.0E-1; 1550.0E-2, etc.

Regardless of the number system, any number in floating point form can be represented by an infinite number of numbers. This form of recording is called unnormalized . For an unambiguous representation of floating point numbers, a normalized form of writing a number is used, in which the mantissa of the number must meet the condition

where |A| - the absolute value of the mantissa of the number.

Condition (2.9) means that the mantissa must be a proper fraction and have a non-zero digit after the decimal point, or, in other words, if the mantissa does not have a zero after the decimal point, then the number is called normalized. So, the number 15.5 in normalized form (normalized mantissa) in floating point form will look like this: 0.155 102, i.e. the normalized mantissa will be A = 0.155 and order P = 2, or in the computer representation of the number 0.155E2 .

Floating point numbers have a fixed format and occupy four (32 bits) or eight bytes (64 bits) of computer memory. If a number occupies 32 bits in the computer's memory, then it is a regular precision number; if it is 64 bits, then it is a double precision number. When writing a floating point number, bits are allocated to store the sign of the mantissa, sign of the exponent, mantissa and exponent. The number of digits allocated to the order of the number determines the range of variation of the numbers, and the number of digits allocated to store the mantissa determines the accuracy with which the number is specified.

When performing arithmetic operations (addition and subtraction) on numbers presented in floating point format, the following procedure (algorithm) is implemented:

1) the order of numbers on which arithmetic operations are performed is aligned (the order of the smaller number in absolute value increases to the order of the larger number in absolute value, the mantissa decreases by the same number of times);

2) arithmetic operations are performed on the mantissas of numbers;

3) the result obtained is normalized.

Practical part