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


| Planning lessons for the academic year (FSES) | § 1.2. Representing numbers in a computer

Lessons 6 - 7
§ 1.2. Representing numbers in a computer

Keywords:

Discharge
unsigned integer representation
signed integer representation
representation of real numbers

1.2.1. Integer representation

A computer's RAM consists of cells, each of which is a physical system consisting of a certain number of homogeneous elements. These elements have two stable states, one of which corresponds to zero, and the other to one. Each such element is used to store one of the bits - a digit of a binary number. That is why each cell element is called a bit or digit (Fig. 1.2).

Rice. 1.2. Memory cell

For computer representation of integers, several different methods are used, differing from each other in the number of digits (integers are usually allocated 8, 16, 32 or 64 digits) and the presence or absence of a sign digit. Unsigned representation can only be used for non-negative integers; negative numbers can only be represented in signed form.

Unsigned representation is used for objects such as cell addresses, various counters (for example, the number of characters in text), as well as numbers indicating date and time, pixel sizes of graphic images, etc.

The maximum value of a non-negative integer is achieved when all bits of the cell contain ones. For n-bit representation it will be equal to 2 n -1. The minimum number corresponds to n zeros stored in n bits of memory and is equal to zero.

The following are the maximum values ​​for unsigned n-bit integers:

To obtain a computer representation of an unsigned integer, it is enough to convert the number to the binary number system and pad the resulting result on the left with zeros to standard digit capacity.

Example 1. The number 53 10 = 110101 2 in eight-digit representation has the form:

The same number 53 in sixteen digits will be written as follows:

When represented with a sign, the most significant (left) digit is assigned to the sign of the number, the remaining digits are assigned to the number itself. If the number is positive, then 0 is placed in the sign bit, if the number is negative - 1. This representation of numbers is called a direct code.

In computers, direct codes are used to store positive numbers in storage devices to perform operations on positive numbers.

The website of the Federal Center for Information and Educational Resources (http://fcior.edu.ru/) contains the information module “Number and its computer code.” With this resource you can get additional information on the topic you are studying.

To perform operations on negative numbers, additional code is used to replace the subtraction operation with addition. You can find out the algorithm for generating an additional code using the information module “Additional Code” located on the website of the Federal Center for Information and Educational Resources (http://fcior.edu.ru/).

1.2.2. Representation of real numbers

Any real number A can be written in exponential form:

Where:

m - mantissa of the number;

p - number order.

For example, the number 472 LLC LLC can be represented as follows: 4.72 10 8, 47.2 10 7, 472.0 10 6, etc.

You might have come across the exponential form of writing numbers when performing calculations using a calculator, when you received entries of the following form as an answer: 4.72E+8.

Here, the sign “E” denotes the base of the decimal number system and is read as “multiply by ten to the power.”

From the example above, you can see that the position of the decimal point in a number can change.

For consistency, the mantissa is usually written as a proper fraction with a non-zero digit after the decimal point. In this case, the number 472 LLC LLC will be represented as 0.472 10 9.

A real number can occupy 32 or 64 bits in computer memory. In this case, bits are allocated to store the mantissa sign, order sign, order and mantissa.

Example:

The range of representation of real numbers is determined by the number of bits allocated to store the order of the number, and the precision is determined by the number of bits allocated to store the mantissa.

The maximum value of the number order for the example above is 1111111 2 = 127 10, and therefore the maximum value of the number is:

0,11111111111111111111111 10 1111111

Try to figure out for yourself what the decimal equivalent of this value is.

A wide range of representations of real numbers is important for solving scientific and engineering problems. At the same time, it should be understood that algorithms for processing such numbers are more labor-intensive compared to algorithms for processing integers.

THE MOST IMPORTANT

To represent integers on a computer, several different methods are used, differing from each other in the number of digits (8, 16, 32 or 64) and the presence or absence of a sign digit.

To represent an unsigned integer, it should be converted to the binary number system and the resulting result should be padded on the left with zeros to standard capacity.

When represented with a sign, the most significant digit is assigned to the sign of the number, the remaining digits are assigned to the number itself. If the number is positive, then 0 is placed in the sign bit; if the number is negative, then 1. Positive numbers are stored in the computer in direct code, negative numbers in complementary code.

When storing real numbers in a computer, bits are allocated to store the sign of the order of the number, the order itself, the sign of the mantissa and mantissa. In this case, any number is written like this:

Where:

m - mantissa of the number;
q - base of the number system;
p - number order.

Questions and tasks

1. Read the presentation materials for the paragraph contained in the electronic appendix to the textbook. Use these materials when preparing answers to questions and completing assignments.

2. How are positive and negative integers represented in computer memory?

3. Any integer can be considered as a real number, but with a zero fractional part. Justify the feasibility of having special ways of computer representation of integers.

4. Represent the number 63 10 in unsigned 8-bit format.

5. Find the decimal equivalents of numbers using their direct codes, written in signed 8-bit format:

a) 01001100;
b) 00010101.

6. Which of the numbers 443 8, 101010 2, 256 10 can be stored in 8-bit format?

7. Write the following numbers in natural form:

a) 0.3800456 10 2;
b) 0.245 10 -3;
c) 1.256900E+5;
d) 9.569120E-3.

8. Write the number 2010.0102 10 in five different ways in exponential form.

9. Write the following numbers in exponential form with a normalized mantissa - a proper fraction that has a non-zero digit after the decimal point:

a) 217.934 10;
b) 75321 10;
c) 0.00101 10.

10. Draw a diagram connecting the basic concepts discussed in this paragraph.

Real numbers in mathematical calculations have no restrictions on the range and precision of number representation. However, in computers, numbers are stored in registers and memory locations with a limited number of digits. That's why accuracy representation real numbers, imaginable in a car, is finite and the range is limited.

When writing real numbers in programs, it is customary to use a dot instead of the usual comma. Any real number can be represented in the form of numbers with the radix order of the number system.

Example 4.4. The decimal number 1.756 in the form of writing numbers with the radix order of the number system can be represented as follows:

1.756 . 10 0 = 0.1756 . 10 1 = 0.01756 . 10 2 = ...

17.56 . 10 -1 = 175.6 . 10 -2 = 1756.0 . 10 -3 = ... .

Floating point representation called number representation N in a number system with a base q as :

N = m* . q p ,

Where m - a multiplier containing all the digits of the number (mantissa), p - an integer called order.

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.

If in the mantissa the first digit after the dot (comma) is different from zero, then such a number is called normalized .

Mantissa and order q It is customary to write an -ary number in the radix system q , and the base itself is in the decimal system.

Example 4.5. Here are examples of a normalized representation of a number in the decimal system:

2178.01 =0.217801 * 10 4

0.0045 =0.45 * 10 -2

Examples in binary:

10110.01= 0.1011001 * 2 101 (order 101 2 = 5 10)

Modern computers support several international standard formats for storing real floating-point numbers, varying in precision, but they all have the same structure. A real number is stored in three parts: the sign of the mantissa, the shifted order and the mantissa:

Characteristic n-bit normalized number is calculated as follows: if the order is allocated k digits, then an offset equal to (2 k -1 -1) is added to the true value of the order represented in the two's complement code.

Thus, an order taking values ​​in the range -128 to +127 is converted into a biased order in the range 0 to 255. The biased order is stored as an unsigned number, which simplifies the comparison, addition and subtraction operations of orders, and also simplifies the comparison operation the normalized numbers themselves.

The number of digits allocated to the order affects the range from the smallest non-zero number to the largest number representable in the machine given the format. Obviously, the more digits allocated to the mantissa, the higher the accuracy of the number representation. Due to the fact that for normalized real numbers the most significant bit of the mantissa is always 1, this most significant bit is not stored in memory.

Any binary integer containing at most m digits, can be converted into real format without distortion.

Table 4.3. Standard formats for representing real numbers

Example 4.6. Representation of normalized numbers in a single format.

Let's illustrate how the number 37.16 10 will be stored. When converting to a binary number, an exact translation of 100101,(00101000111101011100) does not result - the fractional part enclosed in brackets is repeated in the period.

We convert the number into normalized form: 0.100101(00101000111101011100) * 2 110

Let's represent a real number in 32-bit format:

1. The sign of the number is “+”, so we enter 0 in the sign bit (31);

2. To set the order, 8 bits are allocated; to the true value of the order presented in the complementary code, we add the offset (2 7 -1) = 127. Since the order is positive, the direct order code coincides with the additional order, let’s calculate the shifted order: 00000110 + 01111111=10000101

We enter the resulting shifted order.

3. We enter the mantissa, while removing the most significant digit of the mantissa (it is always equal to 1);

shifted order

mantissa

In this example, we were able to transfer only 24 bits; the rest were lost with a loss of precision in representing the number.

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. Therefore:

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 form of representation of floating point numbers 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):

Purpose of the service. The online calculator is designed to represent real numbers in floating point format.

Rules for entering numbers

  1. Numbers in the decimal number system can be entered either without a fractional part or with a fractional part (234234.455).
  2. Numbers in the binary number system consist only of the digits 0 and 1 (10100.01).
  3. Numbers in the hexadecimal number system consist of the digits 0...9 and the letters A...F.
  4. You can also get the reverse representation of the code (from hexadecimal to decimal, 40B00000)
Example No. 1. Represent the number 133.54 in floating point form.
Solution. Let's represent the number 133.54 in normalized exponential form:
1.3354*10 2 = 1.3354*exp 10 2
The number 1.3354*exp 10 2 consists of two parts: the mantissa M=1.3354 and the exponent exp 10 =2
If the mantissa is in the range 1 ≤ M Representing a number in denormalized exponential form.
If the mantissa is in the range 0.1 ≤ M Let's represent the number in denormalized exponential form: 0.13354*exp 10 3

Example No. 2. Represent the binary number 101.10 2 in normalized form, written in the 32-bit IEEE754 standard.
Solution.
Representation of a binary floating point number in exponential normalized form.
Let's shift the number 2 digits to the right. As a result, we obtained the main components of an exponential normalized binary number:
Mantissa M=1.011
Exponent exp 2 =2
Convert binary normalized number to 32-bit IEEE 754 format.
The first bit is allocated to indicate the sign of the number. Since the number is positive, the first bit is 0
The next 8 bits (2nd to 9th) are reserved for the exponent.
To determine the sign of the exponent, in order to avoid introducing another sign bit, add an offset to the exponent of half a byte +127. So our exponent is: 2 + 127 = 129
Let's convert the exponent to binary representation.
The remaining 23 bits are reserved for the mantissa. In a normalized binary mantissa, the first bit is always equal to 1, since the number lies in the range 1 ≤ M To convert the integer part, you need to multiply the digit of the number by the corresponding digit power.
01100000000000000000000 = 2 22 *0 + 2 21 *1 + 2 20 *1 + 2 19 *0 + 2 18 *0 + 2 17 *0 + 2 16 *0 + 2 15 *0 + 2 14 *0 + 2 13 *0 + 2 12 *0 + 2 11 *0 + 2 10 *0 + 2 9 *0 + 2 8 *0 + 2 7 *0 + 2 6 *0 + 2 5 *0 + 2 4 *0 + 2 3 *0 + 2 2 *0 + 2 1 *0 + 2 0 *0 = 0 + 2097152 + 1048576 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 = 3145728
In decimal code, the mantissa is expressed as 3145728
As a result, the number 101.10 represented in IEEE 754 with single precision is equal to.
Let's convert to hexadecimal representation.
Let's divide the source code into groups of 4 bits.
2 = 0100 0000 1011 0000 0000 0000 0000 0000 2
We get the number:
0100 0000 1011 0000 0000 0000 0000 0000 2 = 40B00000 16







2024 gtavrl.ru.