How bit endianness affects bitwise shifts and file IO in C?

前端 未结 5 1932
有刺的猬
有刺的猬 2021-01-02 23:33

Let L and B be two machines. L order its bits from LSB (Least Significant Bit) to MSB (Most Significant Bit) whil

5条回答
  •  庸人自扰
    2021-01-03 00:24

    There isn't really such a thing as bit-endianness, at least as far as C is concerned. CHAR_BIT has to be at least 8 according to the spec, so accesses to any objects smaller than that is pretty much meaningless to a standard C program. Regardless of how the hardware stores a byte - LSB or MSB first - it doesn't affect your program at all. myVar & 1 returns the right bit in either case.

    If you need to interact with some kind of serial interface and reconstitute bytes from it, that's a different story. Your own machine's 'bit-endianness' still doesn't affect anything, but the bit order of the interface certainly does.

    Now, as to your specific question and the program you've shown. Your programs are almost 100% portable. Neither bit- nor byte-endianness affects them. What might affect them is if CHAR_BIT were different on each platform. One computer might write more data than the other one would read, or vice versa.

提交回复
热议问题