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
264
votes
35 answers

How do I convert between big-endian and little-endian values in C++?

How do I convert between big-endian and little-endian values in C++? For clarity, I have to translate binary data (double-precision floating point values and 32-bit and 64-bit integers) from one CPU architecture to another. This doesn't involve…
Uhall
  • 5,673
  • 7
  • 24
  • 19
237
votes
29 answers

Detecting endianness programmatically in a C++ program

Is there a programmatic way to detect whether or not you are on a big-endian or little-endian architecture? I need to be able to write code that will execute on an Intel or PPC system and use exactly the same code (i.e., no conditional…
Jay T
  • 2,379
  • 3
  • 14
  • 3
195
votes
5 answers

Does bit-shift depend on endianness?

Suppose I have the number 'numb'=1025 [00000000 00000000 00000100 00000001] represented: On Little-Endian Machine: 00000001 00000100 00000000 00000000 On Big-Endian Machine: 00000000 00000000 00000100 00000001 Now, if I apply Left Shift on 10 bits…
Sandeep Singh
  • 4,941
  • 8
  • 36
  • 56
170
votes
8 answers

Convert a byte array to integer in Java and vice versa

I want to store some data into byte arrays in Java. Basically just numbers which can take up to 2 Bytes per number. I'd like to know how I can convert an integer into a 2 byte long byte array and vice versa. I found a lot of solutions googling but…
Chris
  • 7,675
  • 8
  • 51
  • 101
122
votes
24 answers

Macro definition to determine big endian or little endian machine?

Is there a one line macro definition to determine the endianness of the machine? I am using the following code but converting it to macro would be too long: unsigned char test_endian( void ) { int test_var = 1; unsigned char *test_endian =…
manav m-n
  • 11,136
  • 23
  • 74
  • 97
114
votes
13 answers

How to convert big endian to little endian in C without using library functions?

I need to write a function to convert a big endian integer to a little endian integer in C. I cannot use any library function. How would I do this?
Alex Xander
  • 3,903
  • 14
  • 36
  • 43
110
votes
9 answers

Does Java read integers in little endian or big endian?

I ask because I am sending a byte stream from a C process to Java. On the C side the 32 bit integer has the LSB is the first byte and MSB is the 4th byte. So my question is: On the Java side when we read the byte as it was sent from the C process,…
hhafez
  • 38,949
  • 39
  • 113
  • 143
108
votes
19 answers

How to check whether a system is big endian or little endian?

How to check whether a system is big endian or little endian?
anand
  • 1,375
  • 5
  • 14
  • 13
102
votes
1 answer

Why is network-byte-order defined to be big-endian?

As written in the heading, my question is, why does TCP/IP use big endian encoding when transmitting data and not the alternative little-endian scheme?
Neji
  • 6,591
  • 5
  • 43
  • 66
97
votes
7 answers

C/C++: Force Bit Field Order and Alignment

I read that the order of bit fields within a struct is platform specific. What about if I use different compiler-specific packing options, will this guarantee data is stored in the proper order as they are written? For example: struct Message { …
dewald
  • 5,133
  • 7
  • 38
  • 42
89
votes
13 answers

How does this program work?

#include int main() { float a = 1234.5f; printf("%d\n", a); return 0; } It displays a 0!! How is that possible? What is the reasoning? I have deliberately put a %d in the printf statement to study the behaviour of printf.
Lazer
  • 90,700
  • 113
  • 281
  • 364
87
votes
4 answers

C program to check little vs. big endian

Possible Duplicate: C Macro definition to determine big endian or little endian machine? int main() { int x = 1; char *y = (char*)&x; printf("%c\n",*y+48); } If it's little endian it will print 1. If it's big endian it will print 0. Is…
ordinary
  • 5,943
  • 14
  • 43
  • 60
84
votes
5 answers

Bitwise operators and "endianness"

Does endianness matter at all with the bitwise operations? Either logical or shifting? I'm working on homework with regard to bitwise operators, and I can not make heads or tails on it, and I think I'm getting quite hung up on the endianess. That…
Frank V
  • 25,141
  • 34
  • 106
  • 144
82
votes
7 answers

Javascript Typed Arrays and Endianness

I'm using WebGL to render a binary encoded mesh file. The binary file is written out in big-endian format (I can verify this by opening the file in a hex editor, or viewing the network traffic using fiddler). When I try to read the binary response…
Bob
  • 909
  • 1
  • 8
  • 8
77
votes
5 answers

Difference between Big Endian and little Endian Byte order

What is the difference between Big Endian and Little Endian Byte order ? Both of these seem to be related to Unicode and UTF16. Where exactly do we use this?
web dunia
  • 9,381
  • 18
  • 52
  • 64
1
2 3
99 100