Real numbers in computer science. Representing numbers in a computer

Theme: Representation of numbers in the computer. Fixed and floating point format. Forward, reverse and complementary code.

Reiteration: Converting integers to binary:

13 10 = and 2 Similarly:

13 10 =1101 2

1345 10 =10101000001 2


The representation of integers in the computer.

All information processed by computers is stored in binary form. How is this storage carried out?

Information entered into a computer and arising in the course of its work is stored in its memory. Computer memory can be thought of as a long page of individual lines. Each such line is called memory cell .

Cell - this is a part of the computer's memory that contains information available for processing a separate team processor. The minimum addressable memory cell is called a byte - 8 binary bits. Byte ordinal is called it address .

cell (8bit \u003d 1byte)

machine word.

A memory cell consists of a number of homogeneous elements. Each element is capable of being in one of two states and serves to display one of the digits of the number. That is why each cell element is called discharge ... It is customary to number the bits in a cell from right to left, the rightmost bit has a serial number 0. This is the least significant bit of the memory cell, the most significant bit has a serial number (n-1) in an n-bit memory cell.

The content of any bit can be either 0 or 1.

The contents of the memory cell are called machine word. The memory cell is divided into bits, each of which stores a digit of the number.

For example, the most modern personal computers are 64-bit, that is, a machine word and, accordingly, a memory cell, consists of 64 bits or bits.

Bit - the minimum unit of measurement of information. Each bit can be 0 or 1. Bit also called discharge computer memory cells.

The standard size of the smallest memory cell is eight bits, that is, eight binary digits. A collection of 8 bits is the basic unit of data representation - a byte.

Byte (from English byte - syllable) - a part of a machine word, consisting of 8 bits, processed in a computer as a whole. On the screen, a memory cell consisting of 8 bits is a byte. The least significant bit has sequence number 0, the most significant bit has sequence number 7.

8 bits \u003d 1 byte

Two formats are used to represent numbers in computer memory: fixed point format and floating point format . In fixed-point format, only integers , in floating point format - real numbers (fractional).

In the overwhelming majority of problems solved with a computer, many actions are reduced to operations on integers. This includes tasks of an economic nature, in the solution of which the data is the number of shares, employees, parts, vehicles, etc. Integers are used to indicate the date and time, and to number various objects: array elements, database records, machine addresses, etc.

Integers can be represented in a computer with a sign or unsigned (be positive or negative).

Unsigned integers usuallyoccupy one or two bytes in memory and accept values \u200b\u200bfrom 00000000 in single-byte format 2 up to 11111111 2 , and in double-byte format - from 00000000 00000000 2 up to 11111111 11111111 2 .

Signed integers usually occupy one, two or four bytes in computer memory, with the leftmost (most significant) bit containing information about the sign of the number. The plus sign is coded as zero, and the minus sign is coded by one.

1101 2 10101000001 2

The category assigned to the sign

(in this case +)

The most significant bits missing to a whole byte are filled with zeros.

In computer technology, three forms of writing (encoding) signed integers are used:straight the code , back the code , additional the code .

Direct code Is a representation of a number in the binary number system, with the first digit assigned to the number sign. If the number is positive, then the first digit contains 0, if the number is negative, the first digit indicates one.

In fact, straight code is used almost exclusively for positive numbers.To write a direct number code, you must:

    Represent a number in binary

    Supplement the number with zeros to the penultimate most significant bit of an 8-bit or 16-bit cell

    Fill in the most significant digit with zero or one, depending on the sign of the number.

Example:number 3 10 in a single-byte format direct code will be represented as:


hislo -3 10 in the direct code of a single-byte format is:


Reverse code for a positive number in the binary system is the same as the direct code. For a negative number, all digits of the number are replaced by their opposite (1 to 0, 0 to 1)invert, and one is entered in the sign bit.

For negative numbers, the so-called complementary code is used. This is due to the convenience of performing operations on numbers by computers.

Additional code used mainly to represent negative numbers in a computer. This code makes arithmetic operations more convenient for computing technology.

In two's complement code, as well as in direct, the first digit is reserved to represent the sign of a number. The forward and complement codes for positive numbers are the same. Since the direct code is used almost exclusively to represent positive numbers, and the complementary code is used for negative numbers, then almost always, if the first bit is 1, then we are dealing with complementary code. (Zero represents a positive number and one represents a negative number.)

Algorithm for obtaining a complementary code for a negative number:

1. Find the direct code of a number (convert a number to the binary number system unsigned number)

2. Get the reverse code. Change every zero to one, and one to zero (invert the number)

3. Add 1 to the reverse code

Example: Let's find the complementary code of the decimal number - 47 in 16-bit format.

    Let's find the binary representation of the number 47 (direct code).

2. Invert this number (reverse code). 3. Add 1 to the reverse code and get a record of this number in RAM.

Important!

For positive numbers, the forward, backward and complementary codes are the same, i.e. straight code. There is no need to invert positive numbers for representation in a computer!

Why is it usedadditional code to represent a negative number?

This makes it easier to perform mathematical operations. For example, we have two numbers represented in straight code. One number is positive, the other is negative, and these numbers need to be added. However, you cannot simply fold them. First, the computer must determine what those numbers are. Having found out that one number is negative, he should replace the addition operation with the subtraction operation. Then, the machine must determine which number is greater in absolute value in order to find out the sign of the result and determine what to subtract from what. As a result, a complex algorithm is obtained. It is much easier to add numbers if negative ones are converted to two's complement.

Practical task:

Exercise 1. Write the forward, backward, and complementary codes of the next decimal numbers using8-bit cell:

64 10, - 120 10

Task 2. Write forward, backward, and complementary codes for the next decimal numbers in a 16-bit grid

57 10 - 117 10 - 200 10

Anyone who has ever thought in life about how to become an "IT specialist" or a system administrator, and simply to connect fate with the knowledge of how numbers are represented is absolutely necessary. After all, this is what low-level programming languages \u200b\u200bsuch as Assembler are based on. Therefore, today we will consider the representation of numbers in a computer and their placement in memory cells.

Notation

If you are reading this article, then most likely you already know about this, but it is worth repeating. All data in a personal computer is 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 to 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 system, you need to take the value we have chosen and divide it by 2. After that, we get the result and the remainder (0 or 1). 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 the remainders in the reverse order as we got 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 minimum unit of information is 1 bit. As we have already found out, the representation of numbers in a computer is 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 chunk of memory can be 1 byte or be an eight-digit binary number.

Whole

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

We will start with the simplest 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, the maximum in one slot can be values \u200b\u200bfrom 0 to 11111111. Let's convert the maximum number into the usual notation form.
X \u003d 1 × 2 7 + 1 × 2 6 + 1 × 2 5 + 1 × 2 4 + 1 × 2 3 + 1 × 2 2 + 1 × 2 1 + 1 × 2 0 \u003d 1 × 2 8 - 1 \u003d 255 ...

Now we see that a value from 0 to 255 can be located in one memory location. However, this applies exclusively to non-negative integers. If the computer needs to write down a negative value, things will go a little differently.

Negative numbers

Now let's see 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 go under the number itself, and the first (leftmost) bit is given under the corresponding sign.

If the digit is negative, then write "1", if positive, then "0". For ease of memorization, you can draw the following analogy: if there is a sign, then put 1, if it is not, then nothing (0).

The remaining 15 bits of information are assigned to a number. Similar to the previous case, a maximum of fifteen units can be placed in them. It is worth noting that the recording of negative and positive numbers differs significantly 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 it was described, and the maximum A \u003d 32766, if you use Immediately I would like to note that in this case "0" refers to positive.

Examples of

Representing integers in computer memory is not such a difficult task. It gets a little more complicated, though, when it comes to negative values. To write a number that is less than zero, a complementary code is used.

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

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

Let's give an illustrative example. Suppose we have a number X \u003d - 131. First, we get its modulus | X | \u003d 131. Then we translate it into the binary system and write it in 16 cells. We get X \u003d 0000000010000011. After inverting, X \u003d 1111111101111100. Add "1" to it and get the reverse code X \u003d 1111111101111101. For writing to a 16-bit memory cell, the minimum number is X \u003d - (2 15) \u003d - 32767.

Long whole

As you can see, representing real numbers in a computer is not that difficult. However, this 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 does not differ at all from the one presented above. So we'll just list the range of numbers that can be stored in this type.

X max \u003d 2 147 483 647.

X min \u003d - 2 147 483 648.

In most cases, these values \u200b\u200bare sufficient to record and perform data operations.

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 insufficient for solving most problems in economics, physics, arithmetic and other sciences. Therefore, now we will consider another technique for supermagnitudes.

Floating point

This is the last thing you need to know about how numbers are represented in a computer. Since when writing fractions there is a problem of determining the position of the comma in them, the computer uses the exponential form to place such numbers.

Any number can be represented in the following form X \u003d m * p p. Where m is the mantissa of the number, p is the base of the number system and n is the order 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 bring it to exponential form. It turns out X \u003d 0.66666 * 10 3. P \u003d 10 and n \u003d 3.

Floating point values \u200b\u200bare usually stored in 4 or 8 bytes (32 or 64 bits). In the first case, this is called a normal precision number, and in the second, double precision.

Of the 4 bytes allocated for storing digits, 1 (8 bits) is given for data on 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.

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

If we now combine both calculations, we get a value that can be written without loss in 4 bytes of memory. It will be equal to X \u003d 1.701411 * 10 38. The rest of the figures were discarded, since this is precisely the accuracy that this recording method allows.

Double precision

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

P \u003d 1111111111 2 \u003d 1023 10.

M \u003d 2 52 -1 \u003d 2 (10 * 5.2) \u003d 1000 5.2 \u003d 10 15.6. We round up and get the maximum number X \u003d 2 1023 with an accuracy of "m".

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

| 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

The computer's operative memory consists of cells, each of which is a physical system, consisting of a 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 bit of a binary number. That is why each element of the cell is called a bit or discharge (Fig. 1.2).

Figure: 1.2. Memory cell

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

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

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

The following are the maximum values \u200b\u200bfor unsigned n-bit integers:

To obtain a computer representation of an unsigned integer, it is enough to translate the number into a binary number system and supplement the resulting result with zeros from the left to the standard bit width.

Example 1... Number 53 10 \u003d 110101 2 in eight-bit representation is:

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

When represented with a sign, the most senior (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 direct code.

In a computer, direct codes are used to store positive numbers in memory 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 the help of this resource you can get additional information on the topic under study.

To perform operations with negative numbers, an additional code is used to replace the operation of subtraction by addition. You can find out the algorithm for generating additional code using the information module "Additional code" posted 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 is the mantissa of the number;

p is the order of the number.

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.

With the exponential form of writing numbers, you might have met when performing calculations using a calculator, when the answer received records of the following form: 4.72E + 8.

Here, the "E" sign denotes the base of the decimal number system and reads as "multiply by ten to the power."

From the above example, you can see that the position of the comma in the number record can change.

For consistency, the mantissa is usually written as a regular fraction with a nonzero digit after the decimal point. In this case, the number 472 LLC LLC would be represented as 0.472 10 9.

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

Example:

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

The maximum order of number for the above example is 1111111 2 \u003d 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 representation 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 time consuming compared to algorithms for processing integers.

THE MOST IMPORTANT THING

For computer representation of integers, 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 a binary number system and padded with zeros on the left to the standard bit width.

When represented with a sign, the most significant bit 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 are stored in the complementary code.

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

Where:

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

Questions and tasks

1. Read the presentation materials for the paragraph contained in the electronic attachment to the textbook. Use these resources to help you answer questions and complete assignments.

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

3. Any integer can be considered as real, but with zero fractional part. Justify the appropriateness of the presence of special methods of computer representation of integers.

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

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

a) 01001100;
b) 00010101.

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

7. Write down 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 down the number 2010.0102 10 in five different ways in exponential notation.

9. Write down the following numbers in exponential form with the normalized mantissa - a regular fraction with a nonzero digit after the decimal point:

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

10. Draw a diagram linking the key concepts discussed in this paragraph.

Real numbers in mathematical calculations have no restrictions on the range and precision of the representation of numbers. However, in computers, numbers are stored in registers and memory locations with a limited number of bits. therefore accuracy representation real numbers,represented in the car, is finite and the range is limited.

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

Example 4.4. The decimal number 1.756 in the form of writing numbers with the base 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 is called the representation of the number N radix q as :

N \u003d m *. q p ,

where m - a factor containing all the digits of a 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 precision of the number representation in the machine.

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

Mantissa and order q -added numbers are usually written in the system with the radix q , and the base itself is in decimal.

Example 4.5. Here are examples of the 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 \u003d 0.1011001 * 2 101 (order 101 2 \u003d 5 10)

Modern computers support several international standard storage formats for floating point numbers, differing 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 selected k bits, then the 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 that takes values \u200b\u200bin the range -128 to +127 is converted to an offset order in the range 0 to 255. The offset order is stored as an unsigned number, which simplifies the comparison, addition, and subtraction of orders, and also simplifies the comparison operation the normalized numbers themselves.

The number of ordering bits affects the range from the smallest nonzero number to the largest number that can be represented in the machine in a given format. Obviously, the more digits are reserved for recording the mantissa, the higher the accuracy of the number representation. Due to the fact that the most significant bit of the mantissa is always 1 for normalized real numbers, this most significant bit is not stored in memory.

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

Table 4.3. Standard formats for representing real numbers

Example 4.6.Single format representation of normalized numbers.

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

We translate the number into a 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, represented in the two's complement, add the offset (2 7 -1) \u003d 127. Since the order is positive, then the direct order code coincides with the complementary one, we calculate the shifted order: 00000110 + 01111111 \u003d 10000101

We enter the resulting shifted order.

3. Enter the mantissa, while removing the highest rank of the mantissa (it is always equal to 1);

shifted order

mantissa

In this example, we were able to transfer only 24 digits, the rest were lost with the loss of the accuracy of the number representation.

Service purpose... The online calculator is designed to represent real numbers in floating point format.

Number entry rules

  1. Numbers in the decimal notation system can be entered both without a fractional part, or with a fractional part (234234.455).
  2. Binary numbers only consist of the digits 0 and 1 (10100.01).
  3. Hexadecimal numbers 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 # 1. Convert 133.54 as a floating point number.
Decision... Let's represent the number 133.54 in normalized exponential form:
1.3354 * 10 2 \u003d 1.3354 * exp 10 2
The number 1.3354 * exp 10 2 consists of two parts: the mantissa M \u003d 1.3354 and the exponent exp 10 \u003d 2
If the mantissa is in the range 1 ≤ M Denormalized exponential representation of a number.
If the mantissa is in the range 0.1 ≤ M Let's represent the number in denormalized exponential notation: 0.13354 * exp 10 3

Example # 2. Represent the binary number 101.10 2 in normalized form, write it down in the 32-bit IEEE754 standard.
Decision.
Exponential Normalized Binary Floating Point Representation.
Let's move the number 2 places to the right. As a result, we got the main components of the exponential normalized binary number:
Mantissa M \u003d 1.011
Exponent exp 2 \u003d 2
Convert normalized binary number to 32 bit IEEE 754 format.
The first bit is reserved for 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, so as not to introduce another sign bit, add a half-byte offset to the exponent +127. So our exponent is: 2 + 127 \u003d 129
Let's translate the exponent into binary representation.
The remaining 23 bits are reserved for the mantissa. For a normalized binary mantissa, the first bit is always 1, since the number lies in the range 1 ≤ M. To translate the integer part, you must multiply the digit of the number by the corresponding degree of the digit.
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 the number 3145728
As a result, the number 101.10 represented in IEEE 754 with single precision is.
Let's translate into hexadecimal notation.
Let's divide the source code into 4-bit groups.
2 = 0100 0000 1011 0000 0000 0000 0000 0000 2
We get the number:
0100 0000 1011 0000 0000 0000 0000 0000 2 \u003d 40B00000 16