zoukankan      html  css  js  c++  java
  • C语言判断系统数据大/小端存储方式

        小端存储:数据的低位部分,存储于存储器的低地址空间里。

        大端存储:数据的低位部分,存储于存储器的高地址空间里。

        首先,一般PC数据存储方式是小端存储。

        基本实现思想是:将存储器中所存的数据按字节以地址顺序输出,与存入数据的高低位进行比较,即得出结论。

    实现方法一:

     1 #include <stdio.h>
     2 int main(void)
     3 {
     4     short int x;
     5     char *arr;
     6     
     7     x = 0x1122;  
     8     arr = (char *)&x;
     9     
    10     if(arr[0]==0x22)
    11         printf("The compute is little-endian.
    ");
    12     else if(arr[0]==0x22)
    13         printf("The compute is big-endian.
    ");
    14     getchar();
    15     return 0;    
    16 }

    实现方法二:

     1 #include <stdio.h>
     2 
     3 union data
     4 {
     5     int inter;
     6     char ch;    
     7 };
     8 
     9 int main(void)
    10 {
    11     union data c;
    12     c.inter = 1;
    13     if(c.ch == 1)
    14         printf("The compute is little-endian.
    ");
    15     else
    16         printf("The compute is big-endian,
    ");
    17         
    18     getchar();
    19     return 0;    
    20 }
  • 相关阅读:
    c++ 的几种强制转换的讨论
    观察者模式
    epoll实现linux进程通信
    vim 实现 go to definition的功能
    svn 的使用
    makefile文件的技术
    [转]epoll技术
    [转]poll技术
    Linux重定向的理解
    避免僵死进程的方法
  • 原文地址:https://www.cnblogs.com/rickhsg/p/3732915.html
Copyright © 2011-2022 走看看