macos 新 Mac 的字节序 - 现在所有的 PC 平台都一样吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/87935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
endian-ness of new macs - are all pc platforms the same now?
提问by Jeff
Does the change of macs over to Intel chips mean we are done with the bit twiddling on numbers in binary resources for cross platform data distributions?
将 mac 换成 Intel 芯片是否意味着我们已经完成了对跨平台数据分发的二进制资源中数字的处理?
Is that the last of this problem or are there some other platforms I'm not aware of?
这是这个问题的最后一个还是还有其他一些我不知道的平台?
回答by 64BitBob
You seem to forget the endianness transcends processor architectures. There are plenty of algorithms and protocols that demand a particular byte order. For example, I spent two weeks trying to get an MD5 hashing algorithm to work, only to realize that I had assumed network byte order (Big Endian) while Ronald Rivest had assumed (without stating so in the RFC) that the implementor would use Little Endian byte order.
您似乎忘记了字节序超越了处理器架构。有很多算法和协议需要特定的字节顺序。例如,我花了两周时间试图让 MD5 散列算法起作用,结果发现我假设了网络字节顺序(Big Endian)而 Ronald Rivest 假设(在 RFC 中没有说明)实现者将使用 Little Endian 字节顺序。
Remind me to hurt that man sometime. :-P
提醒我有时间伤害那个人。:-P
回答by Mecki
Well, actually there are plenty of big endian CPUs left over.
好吧,实际上还有很多 big endian CPU 剩余。
Actually the PPC is not dead. You are aware, that the Xbox360 uses PPC CPUs (and it is a good example, that these CPUs are not as bad as their reputation - the Xbox360 is anything but slow). Okay, this one may not count as a PC.
实际上,PPC 并没有死。您知道,Xbox360 使用 PPC CPU(这是一个很好的例子,这些 CPU 并不像它们的声誉那么糟糕——Xbox360 一点也不慢)。好吧,这可能不算是 PC。
But does a server count as a PC? There are still plenty of servers using Sun's UltraSparc CPUs, that are generally big endian, though the latest models can be either big or little endian. There are many CPUs that can be either one or the other (e.g. ARM, still used in many devices like mobile phones and the like), as supporting both adds greatest flexibility for the hardware and for the software vendors. Even the IA64 CPUs (the Intanium, that was intended to replace x86 before AMD invented x86-64, that was true 64 bit and could only emulate 32 bit, unlike x86-64 that can be both) is one of the CPUs that can be switched to big endian. CPUs that can be both are called bi-endian.
但是,服务器算作 PC 吗?仍然有很多服务器使用 Sun 的 UltraSparc CPU,它们通常是大端的,尽管最新的型号可以是大端或小端。有许多 CPU 可以是一种或另一种(例如 ARM,仍在许多设备中使用,如移动电话等),因为同时支持这两种 CPU 为硬件和软件供应商增加了最大的灵活性。即使是 IA64 CPU(Intanium,在 AMD 发明 x86-64 之前旨在取代 x86,它是真正的 64 位,只能模拟 32 位,不像 x86-64 可以两者兼有)是可以切换到大端。可以同时使用的 CPU 称为双端。
Actually if you ignore Intel (and compatible CPUs) for a second, most CPUs on the market are either big endian or at least bi-endian, though most of these are not used in any consumer PCs as far as I know.
实际上,如果您暂时忽略 Intel(和兼容的 CPU),市场上的大多数 CPU 要么是大端的,要么至少是双端的,尽管据我所知,其中大部分都没有用于任何消费类 PC。
However, I see no endian problem as many programmers do. Every modern CPU can swap endian in hardware. Actually if you'd write a program on a little endian Intel CPU, that swaps endianess of every integer read from memory and again when writing back to memory, this will cause maybe a performance penalty as little as 5%; and in practice you only need to swap endianess for data coming in and going out of your application, as within your application the endianess is constant, of course.
然而,我没有看到像许多程序员那样的字节序问题。每个现代 CPU 都可以在硬件中交换字节序。实际上,如果你在一个小端 Intel CPU 上编写一个程序,它会交换从内存中读取的每个整数的字节序,并在写回内存时再次交换,这可能会导致性能下降 5%;在实践中,您只需要为进出应用程序的数据交换字节序,当然,在应用程序中,字节序是恒定的。
Also note: Almost all network protocols I know specify byte order to be big endian, TCP/IP being the most familiar family. So if you work on lower network layers, you will always have to continue swapping bytes.
另请注意:我知道的几乎所有网络协议都将字节顺序指定为大端,TCP/IP 是最熟悉的家族。因此,如果您在较低的网络层上工作,您将始终必须继续交换字节。
回答by Dinah
I was thinking the same question: since Macs are now Intel, is the endian issue dead? Nope. Aside from certain supercomputers (which, let's face it, us lay-folk will never have to deal with) there is still one major area where big-endian order is used: network protocols, particularly: the Internet Protocol (as in: "IP" of TCP/IP).
我在想同样的问题:既然 Mac 现在是英特尔,那么字节序问题已经死了吗?不。除了某些超级计算机(让我们面对现实,我们外行永远不必处理)还有一个主要领域使用大端顺序:网络协议,特别是:互联网协议(如:“IP ”的 TCP/IP)。
回答by Nick Monkman
This is certainly not the last of this problem, particularly if you are writing for embedded systems, including Pocket PCs, etc. MIPS, ARM, and other architectures support bi-endian architectures which can select their endian-ness on system start-up.
这当然不是这个问题的最后一个,特别是如果您正在为嵌入式系统编写代码,包括 Pocket PC 等。 MIPS、ARM 和其他架构支持双端架构,可以在系统启动时选择它们的端序。
If you're writing code that depends on byte ordering, you need to care about endian-ness. Don't expect this "problem" to go away anytime soon.
如果您正在编写依赖于字节顺序的代码,您需要关心字节序。不要指望这个“问题”会很快消失。
回答by Kevin Conner
Pesky x86's dirtying up my memory registers with their segment pointers! ;)
讨厌的 x86 用它们的段指针弄脏了我的内存寄存器!;)
I believe you don't need to flip words between PCs and Macs anymore, assuming you're eschewing backwards-compatibility with PowerPC.
我相信您不再需要在 PC 和 Mac 之间来回切换,假设您避免向后兼容 PowerPC。
回答by X-Istence
Define PC, what do you consider a PC?
定义PC,您认为PC是什么?
I am currently typing this from an Linux distribution that is running on an arm 9 processor, which can be set into different endianness, but the default is big endian. Little endian is used by Intel, AMD and Via (x86 compatible).
我目前正在从运行在 arm 9 处理器上的 Linux 发行版中键入此内容,可以将其设置为不同的字节序,但默认值为大字节序。Intel、AMD 和 Via(x86 兼容)使用小端。
Endian-ness won't go away any time soon, anytime you transmit anything over the network you have to make sure that it is in the right endianness, since the endian specified by Internet Protocol is actually big endian.
字节序不会很快消失,任何时候你通过网络传输任何东西你都必须确保它是正确的字节序,因为互联网协议指定的字节序实际上是大字节序。
See the Wikipedia article on Endiannessfor more information.
有关详细信息,请参阅Wikipedia 上关于 Endianness 的文章。
回答by SingleNegationElimination
Now, more than ever, a person's main computer is less likely to be a deskop computer running a general purpose operating system. Although that is still quite common, many other folks are using smartphones or umpc devices that are purpose built, ie for browsing the web. These platforms do not neccesarily have x86 cpus. More often, especially with smartphone devices, they are using an ARM core, which is big endian.
现在,一个人的主计算机比以往任何时候都更不可能是运行通用操作系统的台式计算机。尽管这仍然很常见,但许多其他人正在使用专门构建的智能手机或 umpc 设备,即浏览网页。这些平台不一定有 x86 cpu。更多的时候,尤其是智能手机设备,他们使用的是大端的 ARM 内核。