Global Sources
EE Times-India
Stay in touch with EE Times India
EE Times-India > Memory/Storage

Does endianness matter?

Posted: 06 Nov 2015     Print Version  Bookmark and Share

Keywords:embedded systems  LED indicator  graphical display  RAM  RTOS 

There are also other possibilities, like using little-endian within 16-bit words, but storing the 16-bit words inside 32-bit words using big-endian. This is commonly called "middle-endian" or "mixed-endian", but is rarely encountered nowadays. The order of bits within a byte is also potentially arbitrary, but we will ignore that too.

Examples of little-endian CPUs include Intel x86 and Altera Nios II. Big-endian CPUs include Freescale 68K and Coldfire and Xilinx Microblaze. Many modern architectures facilitate both modes and can be switched in software; such "bi-endian" devices include ARM, PowerPC and MIPS.

Consider this code:

unsigned int n = 0x0a0b0c0d;
unsigned char c, d, *p;

c = (unsigned char) n;
p = (unsigned char *) &n;
d = *p;What values would c and d contain at the end? Whatever the endianness, c should contain the value 0x0d. However, the value of d will depend on the endianness. On a little-endian system d will contain 0x0d; on big-endian it will have the value 0x0a. The same kind of effect would be observed if a union were to be made between n and, say, unsigned char a[4] thus:

union e
   unsigned int ui;
   unsigned char a[4];

f.ui = n;
printf("a[0] = 0x%02x\n", f.a[0]);
printf("a[1] = 0x%02x\n", f.a[1]);
printf("a[2] = 0x%02x\n", f.a[2]);
printf("a[3] = 0x%02x\n", f.a[3]);

This code results in this output on a little-endian machine:

a[0] = 0x0d
a[1] = 0x0c
a[2] = 0x0b
a[3] = 0x0a

Does endianness matter?
So, does this matter? With care, most code may be written to be independent of endianness and it may be argued that almost all well-written code would be like this. However, if you do build in an endianness dependency, as usual, good documentation/commenting is obviously essential.

About the author
Colin Walls has over thirty years experience in the electronics industry, largely dedicated to embedded software. A frequent presenter at conferences and seminars and author of numerous technical articles and two books on embedded software, Colin is an embedded software technologist with Mentor Embedded (the Mentor Graphics Embedded Software Division), and is based in the UK.

 First Page Previous Page 1 • 2

Comment on "Does endianness matter?"
*  You can enter [0] more charecters.
*Verify code:


Visit Asia Webinars to learn about the latest in technology and get practical design tips.


Go to top             Connect on Facebook      Follow us on Twitter      Follow us on Orkut

Back to Top