Representation of signed numbers. Representing numbers in a computer


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

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

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)

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

    Add zeros 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

Numerical data is processed in a computer using the binary number system. 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 integer numbers will be from 0 to 25510.

Unlike the decimal system, the binary number system in the computer representation of a binary number does not have symbols indicating the sign of the number: positive (+) or negative (-), therefore, to represent signed integers in the binary system, two number representation formats are used: number value format signed 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, two's complement code is used, which is the modulus of a negative number 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 decimal floating point notation 15.5 is not used when writing computer programs and entering them into a computer (computer input devices accept 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 a smaller absolute number increases to the order of a larger absolute number, while the mantissa decreases by the same amount);

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

3) the result obtained is normalized.

Practical part

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







2024 gtavrl.ru.