Network Infrastructure,  Unix

Big-endian vs. Little-endian

Endianness is the bit/byte ordering (thus aka byte ordering) used to store values, typically in regard to memory and streams of data over a network. This comes up occasionally with heterogenous integration and the bugs that can result. Basically, little-endian is used with the x86 chipset and big-endian is used for the PowerPC line, although in some cases PowerPC can be bi-endian, meaning that it can switch endianness. Well, it doesn’t actually do so from a hardware standpoint but instead when they’re in little-endian mode the software is little-endian, but multi-byte values end up getting swapped to big-endian during memory load.

Big-endian moves up in order – addresses increase upward. In other words if you have a first memory register as 0x0A or 0x0A0B then the next register would be 0x0B or 0x0C0D respectively. Little-endian moves down in order – addresses decrease upward. So if your first memory register is 0x0B or 0x0C0D then the next address would be 0x0A or 0X0A0B. So the addresses space increases in a downward fashion. In networking, endianness is referred to as Network Order. For example, the ordering of IP addresses (and data within most packets for that matter) is handled in a big-endian fashion. But in networking, endianness is pretty much standard across platforms and doesn’t result in issues…

As though it isn’t confusing enough why everyone doesn’t just use the same endianness, there’s actually a Middle-endian, but that’s a whole other weird (and rare) story. As a late comer, middle-endian doesn’t rank much in what has actually been called a Holy War amongst those who do this type of programming, albeit that was 30 years ago, a short Holy War by most people’s views (by the way that article is great if you want to learn more about how registers get written into memory).

How did this issue come about? Some point to hardware. When you’re doing the math there are certain times when you save a lot of resources by going one way or another, especially if the physical components you have will write memory faster if you go one way or another. I’m sure at the time it just made sense to take the path of least resistance and before you knew it you had a whole architecture built on a byte ordering that was difficult to change. Much like in some languages you write left to right and in others you write right to left, the question of endianness really seems to be more of a legacy issue as at this point most platforms use big-endian. With the switch from PowerPC to Intel, Apple no longer has an issue with this kind of stuff, although if you’re running legacy code it still may come up from time to time… Because the x86 platform has pretty much replaced most other platforms this issue comes up less and less every year.