C language "Storage of integers in memory, introduction to big and small endian storage modes"

Table of contents

1. Introduction to data types

2. How integers are stored in memory (original code, inverse code, complement code)

3. Introduction and judgment of big and small endian storage modes


1. Introduction to data types

1. Data types and byte sizes in C language

2. Basic classification of types

        (1) Integer family:

                        

        (2) Floating point family:

                        

        (3) Construction type:

                        

        (4) Pointer type:

                        

        (5) void empty type: usually used for function return types, function parameters, and pointer types.


 2. How integers are stored in memory

We know that the integer type is allocated 4 bytes in memory

Create two integer variables a, b:

                                

 We can use debugging to see its memory storage method

The value of a is 10. The form stored in the memory is as shown in the figure above: 0a 00 00 00. For the convenience of observation, vs displays a hexadecimal number. A is 10. It gives us good feedback, and he Putting the smallest number of byte order bits at the low address is actually the little-endian byte sorting method .

Before talking about how negative numbers are stored in memory, we first introduce the original code, inverse code, and complement code.

        Original code, inverse code, complement code:

        The original code, inverse code and complement code of a positive number are the same. (integer binary)

        The relationship between the original code, complement code and complement code of negative numbers:

                Original code: Convert decimal or other decimal numbers into binary .

                Negative code: The original code except the sign bit (the first bit) remains unchanged, and all other bits are inverted .

                Two's complement: one's complement plus one .

(Supplement: To convert the two's complement code into the original code, you can first subtract one and then invert the bitwise , or you can first invert the bitwise and add one , which is the same as converting the original code into the two's complement code.

All integers are stored in memory in two's complement form .

 

The storage format of -10 in memory is f6 ff ff ff

We can use a computer to calculate -10 in hexadecimal

 As you can see, it is the same as the hexadecimal value stored in the memory above.

If we want to calculate the value of a+b, we cannot calculate it directly using the original codes of a and b, because b is a negative number, and the sign bit of a negative number is 1. In fact, the computer CPU only has addition operations, not For subtraction, if you want to perform a negative number operation, you have to convert it into its complement, and then convert the two complements into the original code to calculate it .

Let’s first write out the original code, inverse code and complement code of -10

Original code: 111111111111111111111111111111111111111111111111111111111110110

Inverse code: 1000000000000000000000000000000000000000000000000001001

1010

The complement calculation process of a+b :

        a:00000000000000000000000000000000000000000000000000001010

        b:10000000000000000000000000000000000000000000000000001010

   a+b:100000000000000000000000000000000000000000000000000000000

Then take 32 bits in the direction of the lower bit of the binary level and remove 1, which is 32 0s, and the result of a+b is 0.


3. Introduction and judgment of big and small endian storage modes

        (1 Introduction

        Little-endian storage mode: means that the numbers with the smallest byte order in the data are stored at the low address, and the numbers with the largest byte order are stored at the high address.

        Big-endian storage mode: means that the numbers with a larger byte order in the data are stored at the lower address, and the numbers with a smaller byte order are stored at the lower address.

        (2) Determine whether the storage mode of the memory in VS is big endian or small segment, and write a piece of code to make the judgment.

        The address is from low to high. We set a = 500 and we can see that the memory storage method is like this,

        

The hexadecimal equivalent of 500 is 1f4

The low byte bits in the data here are placed at the low address

Therefore, the memory storage method in VS is little-endian storage.

(3) Design code to determine endianness:

Principle: Let an integer variable i = 1. There are two ways to store it in memory, namely big endian or small segment.

The top is little endian

Below is the big endian

Since i can access 4 bytes , we need to cast it into char type , let it access 1 byte , and then dereference it . If it is 1, it is little endian , and 0 is big endian .

 

Guess you like

Origin blog.csdn.net/cool_tao6/article/details/130935147