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

Anyone who has ever thought in life about becoming an “IT specialist” or a system administrator, and simply throwing in their lot 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 on 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 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 in the computer. As already mentioned, the first thing the processor does is convert information into binary format, and only then place 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, 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 like to immediately 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 modulus of a negative number is written in binary notation. 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 the accuracy that this recording method allows.

Double precision

Since all the calculations were outlined 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 integers 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.

Subject: Representing numbers on a computer. Fixed and floating point format. Direct, reverse and complementary code.

Repetition: Converting integers to binary number system:

13 10 = A 2 Likewise:

13 10 =1101 2

1345 10 =10101000001 2


Representation of integers in a computer.

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

Information entered into the computer and generated during its operation is stored in its memory. You can think of a computer's memory as a long page made up 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 separate team processor. The minimum addressable memory cell is called a byte - 8 binary digits. The sequence number of a byte is called it address .

cell (8bit = 1byte)

in a machine word.

A memory cell consists of a certain number of homogeneous elements. Each element is capable of being in one of two states and serves to represent one of the digits of a number. That is why each cell element is called discharge . The numbering of digits in a cell is usually done from right to left, the rightmost digit has a serial number 0. This is the low-order digit of the memory cell, the most significant digit has a serial number (n-1) in an n-bit memory cell.

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

The contents of a memory cell are called in a machine word. The memory cell is divided into digits, each of which stores a digit of a 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. Beat also called discharge computer memory cells.

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

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

8 bits = 1 byte

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

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

Integers can be represented in a computer as signed or unsigned (positive or negative).

Unsigned integers usuallyoccupy one or two bytes in memoryand accept values ​​from 00000000 in single-byte format 2 up to 11111111 2 , and in double-byte format - from 00000000 00000000 2 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 as one.

1101 2 10101000001 2

The digit assigned to the sign

(in this case +)

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

In computer technology, three forms of recording (coding) signed integers are used:straight code , back code , additional code .

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

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

    Represent a number in binary

    Complete the number record with zeros up to the penultimate most significant digit of an 8-bit or 16-bit cell

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

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


hislo -3 10 in direct code of a single-byte format it looks like:


Return code for a positive number in the binary number system coincides with the direct code. For a negative number, all digits of the number are replaced with their opposites (1 by 0, 0 by 1)invert, and one is entered in the sign digit.

For negative numbers, the so-called complement code is used. This is due to the convenience of performing operations on numbers with computer technology.

Additional code used primarily to represent negative numbers in a computer. This code makes arithmetic operations more convenient for computers to perform.

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

Algorithm for obtaining the complement code for a negative number:

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

2. Get a return code. Change each zero to a one, and each one to a zero (invert the number)

3. Add 1 to the reverse code

Example: Let's find the additional 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 direct, inverse and complement codes are the same thing, i.e. direct code. There is no need to invert positive numbers to represent them on a computer!

Why is it used?additional code to represent a negative number?

This makes it easier to perform mathematical operations. For example, we have two numbers represented in direct code. One number is positive, the other is negative, and these numbers need to be added. However, you can't just fold them. First the computer has to figure out what the 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 decide what to subtract from what. The result is a complex algorithm. It's much easier to add numbers if the negative ones are converted to two's complement.

Practical task:

Exercise 1. Write down the forward, backward and complement codes of the following decimal numbers using8-bitcell:

64 10, - 120 10

Task 2. Write the forward, reverse and complement codes of the following decimal numbers in a 16-bit grid

57 10 - 117 10 - 200 10

| 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 number 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 allocated to the sign of the number, the remaining digits are allocated 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 information module “Number and its computer code” is posted on the website of the Federal Center for Information and Educational Resources (http://fcior.edu.ru/). 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 may 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.

If we could look into the contents of computer memory, we would see the following:

This figure reflects Rule #1: Data (and programs) in computer memory are stored in binary form, i.e. in the form of chains of zeros and ones.

Rule #2:representation of data in a computer discretely.

What is discreteness?

Closest answer: "Separate"

Note: A discrete set consists of elements separated from each other. For example, sand is discrete because it is made up of individual grains of sand. And water or oil is continuous (within the framework of our sensations, since we still cannot sense individual molecules)

For example, an image is constructed as a collection of points, i.e. discretely.

Rule #3:the set of quantities representable in memory is limited and finite.

Representing numbers on a computer.

Integers in the computer. (Fixed point format)

Any computing device (computer, calculator) can only work with a limited set of integers. Look at the calculator display, it contains 10 characters. The largest positive number that can be placed on the scoreboard:

9

9

9

9

9

9

9

9

9

The largest negative number in absolute value:

9

9

9

9

9

9

9

9

9

The situation is similar in the computer.

For example, if a memory cell of 16 bits is allocated for an integer, then the largest positive number will be like this:

0

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

In the decimal number system it is equal to:

2 15 -1=32767

Here the first bit plays the role of the sign of the number. Zero is a sign of a positive number. The largest absolute negative number is -32768.

How to get its internal representation:

1) convert the number 32768 to the binary number system, it is equal to
1000000000000000 - received direct code.

2) invert this binary code, i.e. replace zeros with ones, and ones with zeros - we got return code.

0111111111111111

3) Add one to this binary number, the result is:

1

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

A one in the first bit denotes a minus sign.

(don't think that the resulting code is "minus zero". This code represents the number -32768.)

These are the rules for machine representation of integers. This internal representation of a number is called additional code.

If N bits are allocated for an integer in the computer memory, then the range of integer values ​​is: [-2 N-1 -1, 2 N -1]

We looked at the format for representing signed integers, i.e. positive and negative. There are times when you only need to work with positive integers. In this case, the format for representing unsigned integers is used.

In this format, the smallest number is zero, and the largest number for a 16-bit cell is:

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

In decimal notation this is 2 16 - 1 = 65535, twice the absolute value of signed notation.

Integers in the computer. (Floating point format)

The largest number may vary from calculator to calculator. The simplest calculator has 999999999. If you add another unit to it, the calculator will display an error message. And on a “smarter” calculator, adding one will lead to the following result:

1

e

+

0

9

This entry on the scoreboard is understood as follows: 1 x10 9.

This number format is called floating point format.

1

e

+

0

9

mantissa

number order

On a computer, numbers can be represented in both fixed point and floating point formats.

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):