美文网首页
Big endian Little endian

Big endian Little endian

作者: 特日根 | 来源:发表于2020-03-18 11:33 被阅读0次

Little and big endian are two ways of storing multibyte data-types ( int, float, etc). In little endian machines, last byte of binary representation of the multibyte data-type is stored first. On the other hand, in big endian machines, first byte of binary representation of the multibyte data-type is stored first.

两种数据存储方式,比如一个int类型总共4个字节(byte),
Little endian: 最后面的字节存在最前面
Big endian: 最前面的字节存在最前


image.png image.png

多数时候这东西不重要,编译器会替程序员管理好这些。但偶尔会需要程序员知道它。

以下代码可以让我们看到int类型在内存的存储方式

#include <stdio.h> 
  
/* function to show bytes in memory, from location start to start+n*/
void show_mem_rep(char *start, int n)  
{ 
    int i; 
    for (i = 0; i < n; i++) 
         printf(" %.2x", start[i]); 
    printf("\n"); 
} 
  
/*Main function to call above function for 0x01234567*/
int main() 
{ 
   int i = 0x01234567; 
   show_mem_rep((char *)&i, sizeof(i)); 
   getchar(); 
   return 0; 
} 

参考文献:
https://www.geeksforgeeks.org/little-and-big-endian-mystery/

相关文章

网友评论

      本文标题:Big endian Little endian

      本文链接:https://www.haomeiwen.com/subject/gsgfyhtx.html