I have a textbook that says:
It is important to understand that in both the big endian and little endian systems, a 32-bit integer with the numerical value of, say, 6, is represented by the bits 110 in the rightmost (low-order) 3 bits of a word and zeros in the leftmost 29 bits.
Is this accurate?
It is true, if you have a
32-bitdata type. Bits000000000000000110will always be6, regardless of little or big endian.This is not true for memory representation (byte level) between little and big endian. In little endian, LSB byte will be first, MSB last.
Imagine a 32-bit number, in
uint32_tvariable, it holds value such asb1<<24 | b2<<16 | b3<<8 | b4wherebxis a byte.b1,b2,b3,b4b4,b3,b2,b1An example using C syntax1:
valuecan either be0x01020304(big) or0x04030201(little), depending on the endianness. (There are systems like PDP-11 where the byte order isn't either of those. PDP Endian and bit shifts is a PDP version of this question on the difference between bit layout in an integer vs. byte access to memory.)Footnote 1: That actually has strict-aliasing undefined behaviour; only safe with
memcpyor compiling with-fno-strict-aliasing, or with compilers like MSVC that define the behaviour even without any options. It also potentially has alignment UB if the compiler happens to align the array by less thanalignof(uint32_t).memcpyalso makes that safe.Going the other way, pointing an
unsigned char*at&valueis well-defined becausechar*andunsigned char*are special types that can alias anything.Of course, the CPU itself doesn't care about C rules; if you're writing in assembly, access to memory or not is explicit and any optimizations are up to the programmer.