当前位置:网站首页>Byte order - how to judge the big end and the small end

Byte order - how to judge the big end and the small end

2022-06-12 11:29:00 Investment paranoia

Byte order - How to judge the big end and the small end

Big end model : The high significant bytes are stored at the low memory address . Network byte order is big end ;

The small end model : The low valid bytes are stored at the low memory address .

Natural order means big end

 Please add a picture description

The principle of judgment : Define a large byte of data x, Judge storage x How about the data of the first address of .

Cast , The pointer points to the first address

Members of the consortium share a section of memory , And they all start from the first address of the memory

/********************************************************* Copyright  2022 Shengkai Liu. All rights reserved. FileName: big_endian.c Author: Shengkai Liu Date: 2022-05-30 ***********************************************************/

#include <stdio.h>

// method one
void is_big_endian()
{
    
    printf(" Method 1 \n Cast :\n");

    int a = 0x11223344;
    char b = *(char *)&a;
    
    if (b == 0x11)
    {
    
        printf(" Big end model \n");
    }
    else
    {
    
        printf(" The small end model \n");
    }
    printf("\n");
}

// method two
void is_big_endian1()
{
    
    printf(" Method 2 \n Consortium :\n");
    union u
    {
    
        int a;
        char b;
    }u;
    u.a = 0x11223344;
    if (u.b == 0x11)
    {
    
        printf(" Big end model \n");
    }
    else
    {
    
        printf(" The small end model \n");
    }
}

int main()
{
    
    printf("size of int: %ld\n", sizeof(int));
    printf("size of char: %ld\n\n", sizeof(char));
    is_big_endian();
    is_big_endian1();
    return 0;
}
原网站

版权声明
本文为[Investment paranoia]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206121123472147.html