我知道如果我想做 2n 那么我可以做 (0x1ul << n)
我会得到结果。
如果处理器是大端的怎么办?如果我向左移动,会不会导致错误的输出?请问(0x1ul << n)
是错误的,即当我们拥有大端架构时不等于 2n?我怎样才能证明这一点?
最佳答案
Endianness only makes sense when you’re breaking up a multi-byte quantity and are trying to store the bytes at consecutive memory locations. However, if you have a 32-bit register storing a 32-bit value, it makes no sense to talk about endianness. The register is neither big-endian nor little-endian; it’s just a register holding a 32-bit value. The rightmost bit is the least significant bit, and the leftmost bit is the most significant bit.
在这种情况下,一旦变量的值从内存加载到寄存器进行移位,它是哪个字节序并不重要,它所做的只是按照指定的移位并存储回内存。
引用:Writing endian-independent code in C
关于c - 移位是否取决于字节序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63293318/