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

How to read integers from a file that are 24bit and little endian using Python?

Is there an easy way to read these integers in? I'd prefer a built in method, but I assume it is possible to do with some bit operations. Cheers edit I thought of another way to do it that is different to the ways below and in my opinion is clearer.…
simonb
  • 2,777
  • 2
  • 18
  • 16
17
votes
8 answers

Does endianness apply to bit order too?

I haven't found a specific question here on SO, if this is a duplicate please point it out to me and I'll delete this. So really, does endianness have anything to do with bit order? This seems to imply the answer is NO, while other sources (I fail…
Bogdan Alexandru
  • 5,394
  • 6
  • 34
  • 54
17
votes
9 answers

Types of endianness

What is the difference between the following types of endianness? byte (8b) invariant big and little endianness half-word (16b) invariant big and little endianness word (32b) invariant big and little endianness double-word (64b) invariant big and…
Ben Lever
  • 2,023
  • 7
  • 26
  • 34
16
votes
7 answers

endian-ness of new macs - are all pc platforms the same now?

Does the change of macs over to Intel chips mean we are done with the bit twiddling on numbers in binary resources for cross platform data distributions? Is that the last of this problem or are there some other platforms I'm not aware of?
Jeff
  • 1,043
  • 1
  • 8
  • 14
16
votes
6 answers

Why does an 8-bit field have endianness?

See the definition of TCP header in /netinet/tcp.h: struct tcphdr { u_int16_t th_sport; /* source port */ u_int16_t th_dport; /* destination port */ tcp_seq th_seq; /* sequence number */ tcp_seq th_ack; …
Claudiu
  • 224,032
  • 165
  • 485
  • 680
16
votes
5 answers

Is using an union in place of a cast well defined?

I had a discussion this morning with a colleague regarding the correctness of a "coding trick" to detect endianness. The trick was: bool is_big_endian() { union { int i; char c[sizeof(int)]; } foo; foo.i = 1; return (foo.c[0] ==…
ereOn
  • 53,676
  • 39
  • 161
  • 238
16
votes
2 answers

How are the union members stored?

union test { int i; char ch; }t; int main() { t.ch=20; } Suppose sizeof(int)==2 and let the memory addresses allocated for t are 2000, 2001. Then where is 20 i.e. t.ch stored - at 2000 or 2001 or depends on endianness of machine?
Happy Mittal
  • 3,667
  • 12
  • 44
  • 60
16
votes
4 answers

Endianness inside CPU registers

I need help understanding endianness inside CPU registers of x86 processors. I wrote this small assembly program: section .data section .bss section .text global _start _start: nop mov eax, 0x78FF5ABC mov ebx,'WXYZ' nop ; GDB…
wrxyz
  • 163
  • 1
  • 1
  • 4
16
votes
3 answers

Endianness conversion in ARM

How do I convert big endian to little endian in ARM?
seyoung
  • 161
  • 1
  • 1
  • 3
16
votes
7 answers

Reversing byte order in .NET

In the code below, why do X and Y take on different values than what I would think intuitively? If the bytes 0-7 are written to the buffer, shouldn't the resulting long have bytes in the same order? It's like it's reading the long values in reverse…
user47589
16
votes
4 answers

Same output for htonl() and ntohl() on an integer

I ran the following program on little-endian [LE] machine [Linux, Intel processor]. I am unable to explain the 3 outputs in below code snippet. Since machine is LE, the value of a is stored as 0x78563412. When printing, it is displaying its actual…
Bhaskar
  • 2,549
  • 1
  • 21
  • 23
15
votes
4 answers

How can I find Endian-ness of my PC programmatically using C?

Possible Duplicate: Detecting endianness programmatically in a C++ program Is there any library function available to find the endian-ness of my PC?
Dinesh
  • 16,014
  • 23
  • 80
  • 122
15
votes
5 answers

Testing for Endianness: Why does the following code work?

While I do understand endianness, I am slightly unclear on how the code works below. I guess this question is less about endianness and more about how the char * pointer and int work i.e. type conversion. Also, would it have made any difference if…
OckhamsRazor
  • 4,824
  • 13
  • 51
  • 88
15
votes
9 answers

Reading "integer" size bytes from a char* array.

I want to read sizeof(int) bytes from a char* array. a) In what scenario's do we need to worry if endianness needs to be checked? b) How would you read the first 4 bytes either taking endianness into consideration or not. EDIT : The sizeof(int)…
kal
  • 28,545
  • 49
  • 129
  • 149
15
votes
3 answers

C# BinaryWriter - and endianness

I am using BinaryWriter in my code, here is my code: static void Main(string[] args) { FileInfo file = new FileInfo(@"F:\testfile"); if (file.Exists) file.Delete(); using (BinaryWriter bw = new BinaryWriter(file.Create())) { …
Eddy Lin
  • 475
  • 2
  • 6
  • 19