6

Will this:

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

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

m0nhawk
  • 22,980
  • 9
  • 45
  • 73
0x10203040
  • 313
  • 2
  • 7
  • 4
    One thing that confuses many people is the fact that endian-ness is only related to *external storage*. Inside the processor's register, there is no "endian-ness". The number is read from the memory following the endian-ness conventions, but inside the register it is the same number, no matter of the initial endian-ness. So when right bit-shifting, you are simply getting rid of least significant bits, and there is no ambiguity. – vsoftco May 15 '15 at 03:55

2 Answers2

11

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.

Barry
  • 286,269
  • 29
  • 621
  • 977
2

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.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190