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

How to byte-swap a 32-bit integer in python?

Take this example: i = 0x12345678 print("{:08x}".format(i)) # shows 12345678 i = swap32(i) print("{:08x}".format(i)) # should print 78563412 What would be the swap32-function()? Is there a way to byte-swap an int in python, ideally with…
Patrick B.
  • 11,773
  • 8
  • 58
  • 101
29
votes
17 answers

Detecting Endianness

I'm currently trying to create a C source code which properly handles I/O whatever the endianness of the target system. I've selected "little endian" as my I/O convention, which means that, for big endian CPU, I need to convert data while writing or…
Cyan
  • 13,248
  • 8
  • 43
  • 78
28
votes
16 answers

Convert Little Endian to Big Endian

I just want to ask if my method is correct to convert from little endian to big endian, just to make sure if I understand the difference. I have a number which is stored in little-endian, here are the binary and hex representations of the…
JeckyPorter
  • 311
  • 1
  • 3
  • 6
28
votes
2 answers

ByteBuffer Little Endian insert not working

I have to make a two way communication between a legacy system and an android device. The legacy system uses little endian byte ordering. I have successfully implemented the receiving part, however sending not works. Strange because for me it seems…
Sandor
  • 1,839
  • 2
  • 18
  • 22
26
votes
3 answers

How to write constexpr swap function to change endianess of an integer?

How to write a constexpr function to swap endianess of an integer, without relying on compiler extensions and can you give an example on how to do it?
user1095108
  • 14,119
  • 9
  • 58
  • 116
26
votes
11 answers

Converting float values from big endian to little endian

Is it possible to convert floats from big to little endian? I have a big endian value from a PowerPC platform that I am sendING via TCP to a Windows process (little endian). This value is a float, but when I memcpy the value into a Win32 float type…
Blade3
  • 4,140
  • 13
  • 41
  • 57
25
votes
2 answers

PNG file format endianness?

Im not sure if endian is the right word but.. I have been parsing through a PNG file and I have noticed that all of the integer values are in big endian. Is this true? For example, the width and height are stored in the PNG file as 32bit unsigned…
Marlon
  • 19,924
  • 12
  • 70
  • 101
25
votes
2 answers

Converting byte array values in little endian order to short values

I have a byte array where the data in the array is actually short data. The bytes are ordered in little endian: 3, 1, -48, 0, -15, 0, 36, 1 Which when converted to short values results in: 259, 208, 241, 292 Is there a simple way in Java to convert…
Johann
  • 27,536
  • 39
  • 165
  • 279
24
votes
2 answers

Is there a meaningful difference between u8::from_be_bytes and u8::from_le_bytes?

Since big-endian and little-endian have to do with byte order, and since one u8 is one byte, wouldn't u8::from_be_bytes and u8::from_le_bytes always have the same behavior?
mpls
  • 395
  • 2
  • 6
24
votes
4 answers

How do I swap endian-ness (byte order) of a variable in javascript

I am receiving and sending a decimal representation of two little endian numbers. I would like to: shift one variable 8 bits left OR them shift a variable number of bits create 2 8 bit numbers representing the first and second half of the 16 bit…
griotspeak
  • 13,022
  • 13
  • 43
  • 54
24
votes
4 answers

Understanding htonl() and ntohl()

I am trying to use unix sockets to test sending some udp packets to localhost. It is my understanding that when setting ip address and port in order to send packets, I would fill my sockaddr_inwith values converted to network-byte order. I am on OSX…
oarfish
  • 4,116
  • 4
  • 37
  • 66
23
votes
3 answers

How to test your code on a machine with big-endian architecture?

Both ideone.com and codepad.org have Little-Endian architechtures. I want to test my code on some machine with Big-Endian architechture (for example - Solaris - which I don't have). Is there some easy way that you know about?
Lazer
  • 90,700
  • 113
  • 281
  • 364
23
votes
4 answers

Marshal.PtrToStructure (and back again) and generic solution for endianness swapping

I have a system where a remote agent sends serialized structures (from an embedded C system) for me to read and store via IP/UDP. In some cases I need to send back the same structure types. I thought I had a nice setup using Marshal.PtrToStructure…
cgyDeveloper
  • 1,901
  • 3
  • 21
  • 34
22
votes
3 answers

How to make GCC generate bswap instruction for big endian store without builtins?

Update: This was fixed in GCC 8.1. I'm working on a function that stores a 64-bit value into memory in big endian format. I was hoping that I could write portable C99 code that works on both little and big endian platforms and have modern x86…
nwellnhof
  • 32,319
  • 7
  • 89
  • 113
22
votes
3 answers

git svn rebase resulted in "byte order is not compatible" error

Following is the error I am getting when I tried 'git svn rebase': Byte order is not compatible at ../../lib/Storable.pm (autosplit into ../../lib/auto/Storable/_retrieve.al) line 380, at /usr/lib/perl5/5.10/Memoize/Storable.pm line 21 The version…
yasouser
  • 5,113
  • 2
  • 27
  • 41