c - 移位是否取决于字节序?

标签 c embedded endianness microprocessors

我知道如果我想做 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/

相关文章:

ubuntu - 在嵌入式设备上的dpkg .deb之后找不到 puppet 代理

c - 位移是否取决于字节顺序?

c - 在运行时动态创建多个字符数组

c - 读取缓冲区分配 (C)

linux - 使用 boost ASIO 配置串口

c - 由 ISR 设置变量的奇怪 PIC 行为

arm - ARM中的字节序转换

memory - 内存中字节序 : Bits in a Byte vs. 字节

c - 在 C 中实现 shellsort 算法

c - Ruby 中没有操作范围?