C language large and small storage

Big and small

  Did you feel confused when you first heard this word? Is the storage order of data in the computer different? What the hell is the big endian? First of all, we must first know a common sense, the big and small are just different storage methods in different hardware, there is no right or wrong, but each has its own advantages and disadvantages. So much has been said, what is the big and small end?

Big endian

  The big-endian storage mode means that the status of the data is stored in the high address of the memory, and the high bit of the data is stored in the low address of the memory.
  For example, an integer 1234, how is it stored in the memory? Take 32 bits as an example, first convert it to binary
  decimal: 1234
  Binary: 100 1101 0010
Insert picture description here
  and there are 32 bite bits in our memory to store integer data, so the storage should be as follows: When the
Insert picture description here  number of bits is not enough , Fill the high bit with 0, then store the high bit data in the low address, and the low bit data in the high address, that is, 00 00 04 D2

Little endian

  Contrary to the big-endian storage mode, the little-endian storage mode means that the low-order bits of the data are stored in the low addresses of the memory, and the high-order bits of the data are stored in the high addresses of the memory.
  Similarly, let's take 1234 as an example.
Insert picture description here  So why is there a distinction between large and small ends? That's because in computer systems, we store data in bytes. Each unit address corresponds to one byte, and one byte corresponds to 8 bits, but our storage of data is more than just Only limited to one byte of the char type, there are also 2-byte short, 4-byte int, and 8-byte long. In addition, for processors with more than 8 bits, such as 16-bit or 32-bit processing As 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.
  After understanding the basic concepts of large and small endian, then we have to learn how to distinguish whether a machine uses big endian or small endian. We can do it with the following small program:

bool check_mode()
{
    
    
	int a = 1;
	return (*(char*)&a) == 1;//取a的低地址位如果为1,则是小端模式,反之则为大端
}

void main()
{
    
    
	bool flag = check_mode();
	if(flag)
		printf("This is little.\n");
	else
		printf("This is big.\n");
}

  Everyone must analyze when facing program calculations and take this situation into consideration.
E.g:

unsigned int i;
for(i = 9; i >= 0; i--)
{
    
    
printf("%u\n",i);
}

  What should be the output of this program? Is it 9 8 7 6 5 4 3 2 1 0?
  The result is often unexpected, it will always output continuously, infinite loop. Because it is an unsigned number, when it is subtracted to 0, the subtraction will be 1111 1111 1111 1111 1111 1111 1111 1111 1111, what a big number this is, and then continue to loop, when it is reduced to 0, It remains the same, so it is an infinite loop.

  Therefore, you must not take it lightly on the small and large end and data storage. . .

Guess you like

Origin blog.csdn.net/dream_i_success/article/details/110203063