Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitshifting on little-endian and big-endian [duplicate]

Will this:

((0x10203040 >> 24) & 0xFF) == 0x10

always be TRUE on both little-endian and big-endian machines?

like image 297
0x10203040 Avatar asked May 14 '15 19:05

0x10203040


People also ask

What is the difference between little endianness and big endianness?

Big-endian is an order in which the "big end" (most significant value in the sequence) is stored first, at the lowest storage address. Little-endian is an order in which the "little end" (least significant value in the sequence) is stored first.

Is there any advantage on choosing Little endian over big-endian?

The advantages of Little Endian are: It's easy to read the value in a variety of type sizes. For example, the variable A = 0x13 in 64-bit value in memory at the address B will be 1300 0000 0000 0000 . A will always be read as 19 regardless of using 8, 16, 32, 64-bit reads.

How is 0x12345678 stored in little endian?

The same is true for a four-byte value; for example, 0x12345678 would be stored as (0x12 0x34 0x56 0x78). Little endian means that the least significant byte of any multi-byte data field is stored at the lowest memory address. This means a Hex word like 0x1234 is stored in memory as (0x34 0x12).

Which is better little endian or big-endian Why?

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.


2 Answers

Yes. Endianness only affects how bytes are stored in memory. The value of 0x10203040 is always 270544960 regardless of whether it's the first or last byte in memory that is the 0x10.

To gratuitously borrow images from the Wikipedia article on Endianness, regardless of which of these layouts our system uses:

enter image description hereenter image description here

the value of 0x0A0B0C0D is still the same.

like image 124
Barry Avatar answered Oct 08 '22 22:10

Barry


will this:

 ((0x10203040 >> 24) & 0xFF) == 0x10

be always TRUE on both little-endian and big-endian machines?

Yes, it will provide the same result on both architectures. It's actually a mathematical operation, and MSB LSB orders will be handled under the hood of that particular CPU's microcode.

like image 34
πάντα ῥεῖ Avatar answered Oct 09 '22 00:10

πάντα ῥεῖ