大端值和小端值可移植吗? [英] Are big endian and little endian values portable?

查看:75
本文介绍了大端值和小端值可移植吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我在小尾数法和大尾数法中都有一个小道消息,我知道这个问题已经问过n次了,但是我不知道下面的几点

Hello i have a small dout in little endian and big endian i know this question has asked n no of times but i could not figure out some below points

let接受int i = 10,它以二进制形式存储为 00000000 00000000 00000000 00001010 在堆栈部分中,如下所示:-

lets take int i=10 it is store in binary as 00000000 00000000 00000000 00001010 in stack section as below:-

00000000 |00000000 |00000000 |00001010   // In case of little endian
MSB-------------------------------------------LSB

大尾数

00001010 |00000000 |00000000 |00000000   // In case of in big endian
MSB-------------------------------------------LSB

在这种情况下,小端和大端将给出相同的输出10?

In this both little and big endian will give same output 10 ?

那么这两个大小端的用途是什么?

Then what is the use of these both little and big endian?

在面试中,我被要求实施可移植到所有大小系统的代码.我回答说:

I was asked to implement code which will be portable for all system that is big or small in my interview. I replied saying:

如果int i = 10在小字节序中然后在大字节序中也是10,则编译器将自行完成操作

compiler will do it self like if int i=10 in little endian then in big endian too it is 10 as output

答案正确吗?

推荐答案

00000000 | 00000000 | 00000000 | 00001010 // big    endian

00001010 | 00000000 | 00000000 | 00000000 // little endian

数据是以大字节序还是小字节序存储的,大多数情况下,只有当您尝试通过指针访问内存中变量的较小部分时才有意义,例如尝试访问32位的最低有效字符通过指向字符的指针或与字符数组的并集的整数.问题的另一个示例是,您是从文件中直接将数据读取到32位整数数组中,还是从32位整数数组中写入数据.文件中的数据通常也将以小字节序或大字节序模式存储.

Whether data is stored in big endian or little endian mode mostly only matters if you're trying to access a smaller portion of a variable in memory, usually via a pointer, like trying to access the least significant character of a 32 bit integer via a pointer to character or a union with a character array. Another example of an issue is if you read data from a file directly into an array of 32 bit integers or if you write data from an array of 32 bit integers. The data in the file will usually be also stored in little endian or big endian mode.

据我所知,尚无通用的编译时方法来确定CPU是在大字节序模式还是小字节序模式下运行(特定的编译器可能已对此进行了定义).您可以使用32位整数的联合和大小为4的字符数组编写测试代码.然后将联合中的整数设置为10,并检查联合字符数组[0]是否包含10,这表示小尾数模式,或者如果联合字符数组[3]包含10,则表示大端模式.可以使用其他方法来确定CPU处于小端模式还是大端模式.

As far as I'm aware, there's no generic compile time method to determine if the cpu is running in big endian mode or little endian mode (specific compilers may have defines for this). You could write test code using a union of 32 bit integer and a character array of size 4. Then set the integer in the union to 10, and check to see if the union character array[0] contains the 10 which means little endian mode, or if the union character array[3] contains the 10, which means big endian mode. Other methods to determine if the CPU is in little endian or big endian mode are possible.

一旦确定cpu处于小字节序还是大字节序模式,就可以包含条件代码来处理这两种情况,例如文件I/O与32位整数数组之间的I/O.如果您希望文件数据处于大端模式,而cpu处于小端模式,则必须在写入文件或从文件读取后反转每个整数的字节.

Once you determine if the cpu is in little endian or big endian mode, you can include conditional code to handle both cases, such as the file I/O to / from an array of 32 bit integers. If you wanted the file data to be in big endian mode, but your cpu is in little endian mode, you'd have to reverse the bytes of each integer before writing or after reading from a file.

您还可以编写代码序列以大端字节序模式存储数据,而与cpu模式无关.如果已经在大端模式下,这会浪费时间,但是对于大端模式和小端模式都有效:

You could also write code sequences to store data in big endian mode, regardless of the cpu mode. It would waste time if already in big endian mode, but it works for both big and little endian mode:

char     buffer[256];
char *   ptr2char;
uint32_t uint32bit;
/* ... */
    ptr2char = buffer;    /* store uint32bit in big endian mode */
    *ptr2char++ = (uint32bit >> 24)&0xff;
    *ptr2char++ = (uint32bit >> 16)&0xff;
    *ptr2char++ = (uint32bit >>  8)&0xff;
    *ptr2char++ = (uint32bit      )&0xff;

这篇关于大端值和小端值可移植吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆