Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Little endian Vs Big endian

Lets say I have 4Byte integer and I want to cast it to 2Byte short integer. Am I right that in both (little and big endian) short integer will consist of 2 least significant bytes of this 4Byte integer?

Second question:
What will be the result of such code in little endian and big endian processor?

int i = some_number;  
short s = *(short*)&i;

IMHO in big endian processor 2 most significant bytes would be copied, and in little endian 2 least significant bytes would be copied.

like image 726
Tomek Tarczynski Avatar asked Feb 11 '10 21:02

Tomek Tarczynski


People also ask

Why is Little Endian better?

The benefit of little endianness is that a variable can be read as any length using the same address. One benefit of big-endian is that you can read 16-bit and 32-bit values as most humans do; from left to right.

What is little endian and big-endian with example?

Little and big endian are two ways of storing multibyte data-types ( int, float, etc). In little endian machines, last byte of binary representation of the multibyte data-type is stored first. On the other hand, in big endian machines, first byte of binary representation of the multibyte data-type is stored first.

Is big or little endian more common?

By far the most common ordering of multiple bytes in one number is the little-endian, which is used on all Intel processors.

Is Windows big or little endian?

The following platforms are considered little endian: AXP/VMS, Digital UNIX, Intel ABI, OS/2, VAX/VMS, and Windows.


2 Answers

Am I right that in both short integer will consist of 2 least significant bytes of this 4Byte integer?

Yes, by definition.

The difference between bigE and littleE is whether the least significant byte is at the lowest address or not. On a little endian processor, the lowest addresses are the least significant bits, x86 does it this way.

These give the same result on little E.

short s = (short)i;
short s = *(short*)&i;

On a big endian processor, the highest addresses are the least significant bits, 68000 and Power PC do it this way (actually Power PC can be both, but PPC machines from Apple use bigE)

These give the same result on big E.

short s = (short)i;
short s = ((short*)&i)[1]; // (assuming i is 4 byte int)

So, as you can see, little endian allows you to get at the least significant bits of an operand without knowning how big it is. little E has advantages for preserving backward compatibility.

So what's the advantage of big endian? It creates hex dumps that are easier to read.

Really, the engineers at Motorola thought that easing the burden of reading hex dumps was more important than backward compatibility. The engineers at Intel believed the opposite.

like image 119
John Knoeller Avatar answered Sep 28 '22 00:09

John Knoeller


  1. Yes. When you convert values, you don't have to worry about endianness.

  2. Yes. When you convert pointers, you do.

like image 34
Seva Alekseyev Avatar answered Sep 28 '22 00:09

Seva Alekseyev