Questions tagged [endianness]

Endianness refers to how multi-byte values are stored in memory, sent between devices or stored on disk. "Big-Endian" values are stored with their most-significant byte first, and "Little-Endian" values are stored with their least-significant byte first. Other byte-orders are possible but very uncommon, and cannot be described this way.

Endianness is the organization and ordering of byte values in multi-byte words. There are two main forms of endianness: big-endian and little-endian.

Big endian (BE) means that the most significant bits are stored first (lowest address). It is similar to reading or saying the name of a decimal number in reading order.

Little endian (LE) means that the least significant bits are stored first. The bytes are stored in reverse of the big-endian format.

There are other forms of byte orderings, but they are rare. They may also be called mixed-endian.

Usage of endianness

When we talk about endian, we often refer to the endianness of an instruction architecture/CPU or the endianness of a file. The endianness of an architecture or a CPU is how the processor organizes the bits in a multi-byte word.

  • Motorola 68000 is a big-endian architecture. It stores multi-byte words in big-endian ordering.
  • Intel processors and the x86 architecture are little-endian.
  • MIPS can run in both big-endian and little-endian format, and you can select the endianness. MIPS is a Bi-endian format.

The endianness of a file indicates how the bytes of a multi-byte word is ordered in a given file (applies both to binary and text files). Sometimes, we indicate the endianness of a file by putting a byte-order mark (BOM) as the first byte of that file.

  • A big-endian UTF-16 text file with BOM would begin with the two bytes FE FF and have all the two-byte characters (each surrogate in a surrogate pair is also one character) be expressed in big endian.
  • A little-endian UTF-16 text file with BOM would begin with the two bytes FF FE and have all the two-byte characters be expressed in little endian.

Examples of endianness

A 32-bit signed int value, 12356789 is stored as four bytes in two's complement format.

  • In big endian, the value is stored as 07 5B CD 15 in hexadecimal notation.
  • In little endian, the value is stored as 15 CD 58 07 in hexadecmial notation.

A UTF-16 text file with BOM contains these characters: A 汉.

  • The BOM character has value U+FEFF. The emoji has Unicode value U+1F197 is expressed as two surrogate pairs, U+D83C U+DD97
  • In big endian, the characters are stored as FEFF 0041 0020 6C49 D83C DD97
  • In little endian, they are stored as FFFE 4100 2000 496C 3CD8 97DD

Read More

Related tags:

External links:

2109 questions
76
votes
12 answers

Determining endianness at compile time

Is there a safe, portable way to determine (during compile time) the endianness of the platform that my program is being compiled on? I'm writing in C. [EDIT] Thanks for the answers, I decided to stick with the runtime solution!
user500944
76
votes
7 answers

Is there any "standard" htonl-like function for 64 bits integers in C++?

I'm working on an implementation of the memcache protocol which, at some points, uses 64 bits integer values. These values must be stored in "network byte order". I wish there was some uint64_t htonll(uint64_t value) function to do the change, but…
ereOn
  • 53,676
  • 39
  • 161
  • 238
75
votes
8 answers

When does Endianness become a factor?

Endianness from what I understand, is when the bytes that compose a multibyte word differ in their order, at least in the most typical case. So that an 16-bit integer may be stored as either 0xHHLL or 0xLLHH. Assuming I don't have that wrong, what…
Anne Quinn
  • 12,609
  • 8
  • 54
  • 101
69
votes
17 answers

64 bit ntohl() in C++?

The man pages for htonl() seem to suggest that you can only use it for up to 32 bit values. (In reality, ntohl() is defined for unsigned long, which on my platform is 32 bits. I suppose if the unsigned long were 8 bytes, it would work for 64 bit…
Tom
  • 21,468
  • 6
  • 39
  • 44
69
votes
6 answers

C# little endian or big endian?

In the documentation of hardware that allows us to control it via UDP/IP, I found the following fragment: In this communication protocol, DWORD is a 4 bytes data, WORD is a 2 bytes data, BYTE is a single byte data. The storage format is little…
TimothyP
  • 21,178
  • 26
  • 94
  • 142
65
votes
3 answers

Why is x86 little endian?

A real question that I've been asking myself lately is what design choices brought about x86 being a little endian architecture instead of a big endian architecture?
bfrog
  • 911
  • 1
  • 9
  • 6
58
votes
9 answers

Why isn't there an endianness modifier in C++ like there is for signedness?

(I guess this question could apply to many typed languages, but I chose to use C++ as an example.) Why is there no way to just write: struct foo { little int x; // little-endian big long int y; // big-endian short z; // native…
Lena Schimmel
  • 7,203
  • 5
  • 43
  • 58
58
votes
4 answers

How do I convert an array of floats to a byte[] and back?

I have an array of Floats that need to be converted to a byte array and back to a float[]... can anyone help me do this correctly? I'm working with the bitConverter class and found myself stuck trying to append the results. The reason I'm doing…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
56
votes
3 answers

Why are both little- and big-endian in use?

Why are both little- and big-endian still in use today, after ~40 years of binary computer-science? Are there algorithms or storage formats that work better with one and much worse with the other? Wouldn't it be better if we all switched to one and…
orlp
  • 112,504
  • 36
  • 218
  • 315
54
votes
12 answers

ASCII strings and endianness

An intern who works with me showed me an exam he had taken in computer science about endianness issues. There was a question that showed an ASCII string "My-Pizza", and the student had to show how that string would be represented in memory on a…
Charles Salvia
  • 52,325
  • 13
  • 128
  • 140
50
votes
3 answers

Why does optimisation kill this function?

We recently had a lecture in university about programming specials in several languages. The lecturer wrote down the following function: inline u64 Swap_64(u64 x) { u64 tmp; (*(u32*)&tmp) = Swap_32(*(((u32*)&x)+1)); …
guitarflow
  • 2,930
  • 25
  • 38
49
votes
3 answers

Floating point Endianness?

I'm writing a client and a server for a realtime offshore simulator, and, as I have to send a lot of data through a socket, I'm using binary data to maximize the amount of data I can send. I already know about integers endianness, and how to use…
cake
  • 1,336
  • 1
  • 13
  • 20
48
votes
13 answers

How to get little endian data from big endian in c# using bitConverter.ToInt32 method?

I am making application in C# which has a byte array containing hex values. I am getting data as a big-endian but I want it as a little-endian and I am using Bitconverter.toInt32 method for converting that value to integer. My problem is that before…
Dany
  • 2,034
  • 8
  • 34
  • 54
48
votes
2 answers

The reason behind endianness?

I was wondering, why some architectures use little-endian and others big-endian. I remember I read somewhere that it has to do with performance, however, I don't understand how can endianness influence it. Also I know that: The little-endian system…
Rad'Val
  • 8,895
  • 9
  • 62
  • 92
46
votes
7 answers

What is the endianness of binary literals in C++14?

I have tried searching around but have not been able to find much about binary literals and endianness. Are binary literals little-endian, big-endian or something else (such as matching the target platform)? As an example, what is the decimal value…
Levi Morrison
  • 19,116
  • 7
  • 65
  • 85