大小端模式测试程序

    技术2025-01-29  28

    #include <stdio.h> typedef union mode { int a;  short b; }m;void testMode(){m c={0x01};if(c.b==0x1000) {  printf("%d/n",c.b);  printf("大端模式/n"); }else if(c.b==0x01) {  printf("%d",c.b);  printf("小端模式/n"); }}int main(){ testMode(); m d={0x1000}; printf("b=%d/n",d.a); return 0;}

     

    说明:因为CPU是三十二位的,所以,一次读入32位数据。

     

    #include <stdio.h>        typedef union mode        {       int a;                short b;        }m;void testMode(){m c={0x12345678}; printf("c.b的十进制是:%x/n",c.b);if(c.b==0x1234)        {                printf("%d/n",c.b);                printf("大端模式/n");        }

    else if(c.b==0x5678)        {                printf("%d",c.b);                printf("小端模式/n");        }}int main(){        testMode();        m d={0x1000};        printf("b=%d/n",d.a);        return 0;}

     

    可以判断是小端模式,d.a=4096

     

    最新回复(0)