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
14
votes
5 answers

What are the benefits of the different endiannesses?

Why did some processor manifacturers decide to use Little endian Big endian Middle endian Any others? ? I've heard that with big endian one can find out faster, if a number is negative or positive, because that bit is the first one. (This doesn't…
Georg Schölly
  • 124,188
  • 49
  • 220
  • 267
14
votes
1 answer

What type of endian does a Macbook Pro with M1 Max use?

Does the new M1 Max ARM Chip use little endian or big endian to store hex?
mdigruber
  • 195
  • 2
  • 10
14
votes
4 answers

Convert uint64_t to byte array portably and optimally in Clang

If you want to convert uint64_t to a uint8_t[8] (little endian). On a little endian architecture you can just do an ugly reinterpret_cast<> or memcpy(), e.g: void from_memcpy(const std::uint64_t &x, uint8_t* bytes) { std::memcpy(bytes, &x,…
Timmmm
  • 88,195
  • 71
  • 364
  • 509
14
votes
3 answers

which CPUs support MOVBE instruction?

Sometimes GCC generates this instruction when compiling with -march=atom. Does each and every Intel Atom CPU support MOVBE? What other processors support this instruction? I can't seem to find this information on Intel website. Please help.
Gart
  • 2,297
  • 1
  • 28
  • 36
14
votes
2 answers

Finding endian-ness programmatically at compile-time using C++11

I have referred many questions in SO on this topic, but couldn't find any solution so far. One natural solution was mentioned here: Determining endianness at compile time. However, the related problems mentioned in the comments & the same…
iammilind
  • 68,093
  • 33
  • 169
  • 336
14
votes
3 answers

Getting pixel format from CGImage

I understand bitmap layout and pixel format subject pretty well, but getting an issue when working with png / jpeg images loaded through NSImage – I can't figure out if what I get is the intended behaviour or a bug. let nsImage:NSImage =…
Ian Bytchek
  • 8,804
  • 6
  • 46
  • 72
14
votes
2 answers

Endianness in constexpr

I want to create a constexpr function that returns the endianness of the system, like so: constexpr bool IsBigEndian() { constexpr int32_t one = 1; return (reinterpret_cast(one) == 0); } Now, since the function will get…
Rick de Water
  • 2,388
  • 3
  • 19
  • 37
14
votes
3 answers

Is Little-Endianness a byte order or a bit order in the x86 architecture?

The title says it all. I want to know whether an x86 instruction that reads data from memory, translates its bytes or its bits to the Little-Endian order. For example if we have the following data at address 0 (in binary) (written here in RAW…
AnArrayOfFunctions
  • 3,452
  • 2
  • 29
  • 66
13
votes
6 answers

What makes a system little-endian or big-endian?

I'm confused with the byte order of a system/cpu/program. So I must ask some questions to make my mind clear. Question 1 If I only use type char in my C++ program: void main() { char c = 'A'; char* s = "XYZ"; } Then compile this program…
kev
  • 155,172
  • 47
  • 273
  • 272
13
votes
5 answers

In Java, when writing to a file with DataOutputStream, how do I define the Endian of the data being written?

I'm using DataOutputStream to write to a file, however I want to change the endian of the data. This is how i'm writing the byte data to the file (it outputs in Little endian by default) public void generateBinObjFile(String outputFile) try { …
Zeeno
  • 2,671
  • 9
  • 37
  • 60
13
votes
8 answers

Command-line to reverse byte order/change endianess

I'm hacking around in some scripts trying to parse some data written by Javas DataOutputStream#writeLong(...). Since java always seems to write big endian, I have a problem feeding the bytes to od. This is due to the fact that od always assumes that…
Alexander Torstling
  • 18,552
  • 7
  • 62
  • 74
13
votes
6 answers

JavaScript Endian Encoding?

A response on SO got me thinking, does JavaScript guarantee a certain endian encoding across OSs and browsers? Or put another way are bitwise shifts on integers "safe" in JavaScript?
Allain Lalonde
  • 91,574
  • 70
  • 187
  • 238
13
votes
2 answers

Little-Endian Signed Integer

I know the WAV file format uses signed integers for 16-bit samples. It also stores them in little-endian order, meaning the lowest 8 bits come first, then the next, etc. Is the special sign bit on the first byte, or is the special sign bit always on…
Leo Izen
  • 4,165
  • 7
  • 37
  • 56
13
votes
3 answers

Comparison of byte literals in Python

The following question arose because I was trying to use bytes strings as dictionary keys and bytes values that I understood to be equal weren't being treated as equal. Why doesn't the following Python code compare equal - aren't these two…
Matthew Hemke
  • 163
  • 1
  • 1
  • 5
13
votes
1 answer

what does ntohs() in pcap exactly do?

I read the documentation from one of the answers: The ntohs function takes a 16-bit number in TCP/IP network byte order (the AF_INET or AF_INET6 address family) and returns a 16-bit number in host byte order. Please explain with an example, as in…
tabs_over_spaces
  • 352
  • 1
  • 3
  • 14