Storage of integers and floating point numbers in memory

 

Create a variable, the system will automatically open a space for it in the memory, the size of the space varies according to the type. So how is the data stored in the memory space opened up?


1. The storage of shaping in memory

1. The complement of the original code and the inverse code

1.1 Concept

Original code: a binary sequence of numbers

Inverse code: the sign bit remains unchanged, and the original code is reversed bit by bit (sign bit: 0 means positive number, 1 means negative number)

Complement: One's complement plus one

Example: The complement of the original code of 10 is as follows:

1.2 In the computer system, in order to make the hardware simpler , the value is always stored in the form of complement (represented in hexadecimal)

①The sign bit and the value field can be processed uniformly;

②Addition and subtraction can also be processed in a unified manner (CPU only has an addition processor)

③Mutual conversion between the complement code and the original code, the operation process is the same.

           Invert the original code +1->complement code 

           Inverted complement +1->original code

2. Endianness

In computer systems, we are in bytes of each address corresponds to a byte unit, a 8bit byte . But in the C language, in addition to the 8bit char, there are 16bit short type, 32bit long type (depending on the specific compiler), and for processors with more than 8 bits, such as 16-bit or 32-bit Because the register width is greater than one byte, there must be a problem of arranging multiple bytes. This led to the big-endian storage model and the little-endian storage model.

The actual content in the memory is expressed in hexadecimal. One hexadecimal is 4 bits, and two hexadecimals are 8 bits, which is exactly one byte.

2.1 Little endian byte order : the high order is placed at the high address (little, status, low address, little end)

2.2 Big-endian byte order : the high bit is placed at the low address

 

The endianness of a machine depends on the CPU, and both endianness machines exist, but the little-endian machine is the mainstream on the PC~

Second, the storage of floating-point numbers in memory

About int occupies a few bytes in memory: it is system related.

Regarding the number of bytes that float and double occupy in memory: The IEEE 754 standard specifies how floating-point numbers are stored

1.Float

(Single-precision floating-point number storage model)

2.Double

(Double-precision floating-point number storage model)

3. Compare Float and Double

In the above figure, S is the sign bit (0 means positive +, 1 means negative -), E exponent part, M significant digits

which is:+/- M*2^E

The more bits occupied by M, the higher the data accuracy; the more bits occupied by E, the larger the data range.

The precision and range of double are larger than float , double is preferred in actual development

When floating-point numbers are stored in memory, there are often errors. In actual development, you cannot use == comparison between floating-point numbers; if you need to determine that the floating-point numbers are equal, you need to make a difference, and then see if the difference is a value smaller than the expected error. If the error meets our requirements, it can be approximately regarded as equal.

When absolute precision is required, avoid using double and float~~

 

Guess you like

Origin blog.csdn.net/weixin_43939602/article/details/109675157