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

Anyone who has ever thought about becoming an IT professional or system administrator, or simply associating fate with computer technology, knowing how numbers are represented in computer memory is absolutely necessary. Indeed, this is where low-level programming languages ​​such as Assembler are based. Therefore, today we will consider the representation of numbers in a computer and their placement in memory cells.

representation of numbers in a computer

Number system

If you are reading this article, then most likely you already know about it, but it’s worth repeating. All data in a personal computer is stored in a binary number system. This means that any number must be represented in the appropriate form, that is, consisting of zeros and ones.

To translate the usual decimal numbers to a form that is understandable to a computer, you need to use the algorithm described below. There are also specialized calculators.

So, in order to translate the number into a binary number system, you need to take the value we selected and divide it by 2. After that, we will get the result and the remainder (0 or 1). Divide the result again 2 and remember the remainder. This procedure must be repeated until in the end there is also 0 or 1. Then we write down the final value and the residues in the reverse order, as we received them.

That is how the representation of numbers in a computer occurs. Any number is written in binary form, and then it occupies a memory cell.

representation of real numbers in a computer

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 takes place in binary format. Thus, each bit of memory will be occupied by one value - 1 or 0.

Cells are used to store large numbers . 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 got to the direct placement of data on the computer. As already mentioned, the processor first converts the 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 takes a ridiculously small number of cells for this process - just one. Thus, the maximum in one slot can be values ​​from 0 to 11111111. Let's translate the maximum number into the familiar form of writing.
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 a value from 0 to 255 can be located in one memory cell. However, this applies exclusively to non-negative integers. If the computer needs to write a negative value, everything will go a little differently.

representation of numbers in computer memory

Negative numbers

Now let's see how numbers are represented on a computer if they are negative. To place 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 number is negative, then "1" is written, if it is positive, then "0". For ease of memorization, we can draw an analogy: if there is a sign, then put 1, if it does not, then nothing (0).

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

In order to place a value greater than zero or equal to it 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 is A = 32766, if you use the decimal number system. Just want to note that in this case, "0" refers to positive.

representation of integers in a computer

Examples

Representing integers in computer memory is not such a difficult task. Although it is a little more complicated when it comes to a negative value. To write a number that is less than zero, an additional code is used.

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

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

We give a clear example. Suppose we have the number X = - 131. First we get its module | X | = 131. Then we translate it into a binary system and write it in 16 cells. We get X = 0000000010000011. After inverting X = 1111111101111100. Add "1" to it and get the return code X = 1111111101111101. For writing to a 16-bit memory cell, the minimum number is X = - (2 15 ) = - 32767.

Long integers

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

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

X max = 2 147 483 647.

X min = - 2 147 483 648.

representation of integers in computer memory

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

The representation of 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 greatly speeds up the processor. On the other hand, this range is not enough to solve most problems of economics, physics, arithmetic and other sciences. Therefore, now we will consider the next methodology for super quantities.

Floating point

This is the last thing you need to know about representing numbers on a computer. Since the problem of determining the position of the comma in them arises when writing fractions, an exponential form is used to place such numbers in the computer.

Any number can be represented in the following form X = 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 notation of floating point numbers, the following condition is used, according to which the mantissa module must be greater than or equal to 1 / n and less than 1.

Let us be given the number 666.66. We bring it to exponential form. It turns out X = 0.66666 * 10 3 . P = 10 and n = 3.

4 or 8 bytes (32 or 64 bits) are usually allocated for storing floating point values. In the first case, this is called the number of ordinary accuracy, and in the second - double precision.

Of the 4 bytes allocated for storing the numbers, 1 (8 bits) is given for data on the order and its sign, and 3 bytes (24 bits) go to the storage of the mantissa and its sign according to the same principles as for integer values. Knowing this, we can make simple calculations.

The maximum value of 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 got an approximate value.

If we now combine both calculations, we get a value that can be recorded without loss in 4 bytes of memory. It will be equal to X = 1,701411 * 10 38 . The remaining numbers were discarded, since it is precisely this accuracy that allows this recording method.

representation of integers and real numbers in a computer

Double precision

Since all the calculations were described and explained in the previous paragraph, here we will tell you very briefly. For double-precision numbers, 11 digits for the order and its sign are usually allocated, as well as 53 digits 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 with an accuracy of "m".

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

Source: https://habr.com/ru/post/C38106/


All Articles