小端和大端的混淆
Confusion over little and big endian
我正在阅读一篇解释小端和大端之间区别的文章。我知道 big endian 首先存储数据“big end”,而 little endian 首先存储数据“little end”。我的困惑在于以下文本块:
Big endian machine: I think a short is two bytes, so I'll read them off: location s is address 0 (W, or 0x12) and location s + 1 is address 1 (X, or 0x34). Since the first byte is biggest (I'm big-endian!), the number must be 256 * byte 0 + byte 1, or 256*W + X, or 0x1234. I multiplied the first byte by 256 (2^8) because I needed to shift it over 8 bits.
我不明白他们为什么要进行 8 位的位移。
另外,这是我不明白的另一段文字:
On a big endian machine we see:
Byte: U N I X
Location: 0 1 2 3
Which make sense. U is the biggest byte in "UN" and is stored first. The > same goes for IX: I is the biggest, and stored first.
On a little-endian machine we would see:
Byte: N U X I
Location: 0 1 2 3
如果我的理解是正确的,在小端机器上不就是“INUX”吗?
完整文章位于 https://betterexplained.com/articles/understanding-big-and-little-endian-byte-order/.
如果有人能解决这个问题,那就太好了。
好的,我明白了大端和小端的工作原理了:
我将解决我在理解第二段文本时遇到的问题。
基本上,在文章中,作者声明如果我们将单词 "UNIX, " 存储为一对 shorts
(而不是 long
),那么最终结果将是 "NUXI."
我现在将解决我理解第一段文本的问题。
基本上,进行位移是为了切换内存中字节的排列,因此,在大端字节序的情况下,最高有效字节在前,而在小端字节序中,最低有效字节在前。
我正在阅读一篇解释小端和大端之间区别的文章。我知道 big endian 首先存储数据“big end”,而 little endian 首先存储数据“little end”。我的困惑在于以下文本块:
Big endian machine: I think a short is two bytes, so I'll read them off: location s is address 0 (W, or 0x12) and location s + 1 is address 1 (X, or 0x34). Since the first byte is biggest (I'm big-endian!), the number must be 256 * byte 0 + byte 1, or 256*W + X, or 0x1234. I multiplied the first byte by 256 (2^8) because I needed to shift it over 8 bits.
我不明白他们为什么要进行 8 位的位移。 另外,这是我不明白的另一段文字:
On a big endian machine we see:
Byte: U N I X Location: 0 1 2 3
Which make sense. U is the biggest byte in "UN" and is stored first. The > same goes for IX: I is the biggest, and stored first.
On a little-endian machine we would see:
Byte: N U X I Location: 0 1 2 3
如果我的理解是正确的,在小端机器上不就是“INUX”吗? 完整文章位于 https://betterexplained.com/articles/understanding-big-and-little-endian-byte-order/.
如果有人能解决这个问题,那就太好了。
好的,我明白了大端和小端的工作原理了:
我将解决我在理解第二段文本时遇到的问题。
基本上,在文章中,作者声明如果我们将单词 "UNIX, " 存储为一对 shorts
(而不是 long
),那么最终结果将是 "NUXI."
我现在将解决我理解第一段文本的问题。
基本上,进行位移是为了切换内存中字节的排列,因此,在大端字节序的情况下,最高有效字节在前,而在小端字节序中,最低有效字节在前。