难道位移取决于字节序? [英] Does bit-shift depend on endianness?

查看:288
本文介绍了难道位移取决于字节序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有多少'麻木'= 1025 [00000000 00000000 00000100 00000001] 重新presented:

Suppose I have the number 'numb'=1025 [00000000 00000000 00000100 00000001] represented:

在小端机:

00000001 00000100 00000000 00000000

在大端机:

00000000 00000000 00000100 00000001

现在,如果我申请上留下10位的移位(即:麻木<< = 10),我应该有:

Now, if I apply Left Shift on 10 bits (i.e.: numb <<= 10), I should have:

[A]在小端机:

我在GDB注意到,小端的确在3个步骤左Shift:我已经展示了3的步骤,以便更好地了解处理只有]

As I noticed in GDB, Little Endian does the Left Shift in 3 steps: [I have shown '3' Steps to better understand the processing only]


  1. 对待没有。大端公约:

  1. Treat the no. in Big-Endian Convention:

00000000        00000000        00000100    00000001


  • 应用左移:

  • Apply Left-Shift:

    00000000        00010000        00000100        00000000
    


  • 在小端重新present的结果再次:

  • Represent the Result again in Little-Endian:

    00000000        00000100        00010000        00000000 
    


  • [B]。在大端机:

    00000000        00010000        00000100        00000000
    


    我的问题是:


    My Question is:

    如果我直接申请左移上的Little Endian
    公约,它应该给:

    If I directly apply a Left Shift on the Little Endian Convention, it should give:

    麻木

    00000001 00000100 00000000 00000000
    

    麻木&LT;&LT; 10

    00010000 00000000 00000000 00000000
    

    但实际上,它给了:

    But actually, it gives:

    00000000        00000100        00010000        00000000 
    

    要实现第二个结果而已,我已经展示了上述三个假设的步骤。

    To achieve the second result only, I have shown three hypothetical steps above.

    请解释一下我为什么上面的两个结果是不同的:麻木1所述的实际结果;&LT; 10 比预期的结果不同。

    Please explain me why the above two results are different: The actual outcome of numb << 10 is different than the expected outcome.

    推荐答案

    端序是值存储在存储器中的方法。当加载到处理器,无论字节序的,位移位指令在处理器的寄存器中的值操作。因此,从内存到处理器的加载转换为大端相当于,在换档操作随之而来的,然后将新值存回内存,这哪里是Little Endian字节序生效一次。

    Endianness is the way values are stored in memory. When loaded into the processor, regardless of endianness, the bit shift instruction is operating on the value in the processor's register. Therefore, loading from memory to processor is the equivalent of converting to big endian, the shifting operation comes next and then the new value is stored back in memory, which is where the little endian byte order comes into effect again.

    这篇关于难道位移取决于字节序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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