Rotations and endians

The question is pretty simple: does 1_u8.rotate_left(1) produce the same value-result (which should be 2) on big-endian and little-endian machines? Same question for shifts, and rotate_right(). Searcheed hardly in docs, but didn't find the answer. Playground has little endian, no BE option.

Yes. Bitwise operations operate on the number as an abstract concept; they behave the same no matter how it is stored in memory. For example, 3_u16 is always equivalent to the binary 0b0000000000000011 regardless of hardware, and 3_u16.rotate_right(1) always returns 0b1000000000000001 (i.e., 32769).

Differences in endianness only becomes observable when you view individual bytes of a multi-byte number, by transmuting it to an array of bytes or by dereferencing pointers into its location in memory.

(For single-byte values like u8, there are never any differences at all between little-endian and big-endian architectures.)


Thanks! I was so stuck on this.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.