Friday, 9 December 2011

Endianness: how does CPU read bytes from memory?

The simplest example is enough to give you the idea: CPU needs to read 2-byte data type from memory. The following example shows how do Big and Little endian machines read variable of type unsigned short from memory:

main.cpp



Output:

Memory layout:
arr[0] = 0xab
arr[1] = 0xcd
val (simple cast on little endian host) = 52651
val (as interpreted by little endian cpu) = 52651
val (as interpreted by big endian cpu) = 43981

No comments: