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
13
votes
3 answers

Portable serialisation of IEEE754 floating-point values

I've recently been working on a system that needs to store and load large quantities of data, including single-precision floating-point values. I decided to standardise on network byte order for integers, and also decided to store floating point…
Peter T.B. Brett
  • 1,250
  • 11
  • 20
13
votes
5 answers

Endianness swap without ntohs

I am writing an ELF analyzer, but I'm having some trouble converting endianness properly. I have functions to determine the endianness of the analyzer and the endiannness of the object file. Basically, there are four possible scenarios: A big…
xdumaine
  • 10,096
  • 6
  • 62
  • 103
12
votes
5 answers

Converting 8 bytes of little-endian binary into a double precision float

I have a binary file that I read byte by byte. I come across a section that is 8 bytes long, holding a double precision float (little endian). I can't figure out how to read this in and calculate it properly with masking and/or casting. (To be…
RedLeader
  • 657
  • 1
  • 15
  • 28
12
votes
1 answer

Is it possible to use std::endian if it is available, otherwise do something else?

Since C++20 we can have: constexpr bool is_little_endian = std::endian::native == std::endian::little; I would like to have code that does this if it is available, otherwise does runtime detection. Is this possible? Maybe the code would look…
M.M
  • 138,810
  • 21
  • 208
  • 365
12
votes
3 answers

In UTF-16, UTF-16BE, UTF-16LE, is the endian of UTF-16 the computer's endianness?

UTF-16 is a two-byte character encoding. Exchanging the two bytes' addresses will produce UTF-16BE and UTF-16LE. But I find the name UTF-16 encoding exists in the Ubuntu gedit text editor, as well as UTF-16BE and UTF-16LE. With a C test program I…
hao.zhou
  • 131
  • 1
  • 1
  • 4
12
votes
2 answers

Finding if the system is little endian or big endian with perl

Is there an option to find if my system is little endian byte order or big endian byte order using Perl?
oren
  • 479
  • 1
  • 6
  • 10
12
votes
6 answers

Can someone explain this "endian-ness" function for me?

Write a program to determine whether a computer is big-endian or little-endian. bool endianness() { int i = 1; char *ptr; ptr = (char*) &i; return (*ptr); } So I have the above function. I don't really get it. ptr = (char*) &i,…
SIr Codealot
  • 5,331
  • 9
  • 33
  • 45
12
votes
4 answers

Is C# Endian sensitive?

Is C# ever Endian sensitive, for example, will code such as this: int a = 1234567; short b = *(short*)&i; always assign the same value to b. If so, what value will it be? If not, what good ways are there to deal with endianness if code with…
Martin
  • 12,469
  • 13
  • 64
  • 128
12
votes
8 answers

Getting 32 bit words out of 64-bit values in C/C++ and not worrying about endianness

It's my understanding that in C/C++ bitwise operators are supposed to be endian independent and behave the way you expect. I want to make sure that I'm truly getting the most significant and least significant words out of a 64-bit value and not…
Jay Atkinson
  • 3,279
  • 2
  • 27
  • 41
12
votes
2 answers

Why is SQL Server Big Endian?

From what I've read, all Windows versions and .NET are little endian. So why the departure from the Microsoft norm for SQL Server? What I mean by "SQL Server is big endian" is this: SELECT CONVERT(VARBINARY, 255); gives: 0x000000FF and…
Atario
  • 1,371
  • 13
  • 24
11
votes
2 answers

What is the difference between big and little endian floats?

For integer values, it is pretty straightforward the difference in little endian and big endian representation. But it is not clear for me how a little endian float differs from a big endian float. And finally, I would like to know which is more…
André Puel
  • 8,741
  • 9
  • 52
  • 83
11
votes
1 answer

Is iOS guaranteed to be little-endian?

It appears that ARM processors can be configured as big-endian or little-endian. However, according to the interwebs, ARM processors are "almost always" configured as little-endian. Is it guaranteed that iOS will run an ARM processor in…
Robert Dodier
  • 16,905
  • 2
  • 31
  • 48
11
votes
4 answers

Fast reading of little endian integers from file

I need to read a binary file consisting of 4 byte integers (little endian) into a 2D array for my Android application. My current solution is the following: DataInputStream inp = null; try { inp = new DataInputStream(new BufferedInputStream(new…
Mad Scientist
  • 18,090
  • 12
  • 83
  • 109
11
votes
3 answers

Is it safe to detect endianess with union?

In other words, according to the C standard, is this code safe? (Assume uint8_t is one byte) void detectEndianness(void){ union { uint16_t w; uint8_t b; } a; a.w = 0x00FFU; if (a.b == 0xFFU) { puts("Little…
iBug
  • 35,554
  • 7
  • 89
  • 134
11
votes
2 answers

Detect Endianness with CMake

My library needs to read-in big-endian integers (4-bytes) and convert them to the endian order of the host for processing. While on *nix ntohl has worked a treat under Windows use of ntohl requires the use of Ws2_32.dll (Winsock). Such a dependency…
Freddie Witherden
  • 2,369
  • 1
  • 26
  • 41