-5

I know that endianess little or big is inherent to the system. Then, how is it possible to change using c code, as I have seen a code, which says it can change the endianess.

abinjacob
  • 31
  • 6
  • "I have seen a code, which says it can change the endianess" --> post that code. – chux - Reinstate Monica Sep 02 '16 at 12:45
  • Hello Chux, its something like....../* * convert big -little endian and conversly * this function assumes that sizeof(unsigned int) == 4 */ unsigned int endian_swap(unsigned int x) { return (x>>24) | ((x>>8) & 0x0000ff00) | ((x<<8) & 0x00ff0000) | (x<<24); } – abinjacob Sep 02 '16 at 16:03
  • The endian-ness of the _system_ is not changed by that code. The bytes of a single `unsigned` are re-ordered. That one number has a new value as the value of a `unsigned` is still interpreted (like `printf("%x", ...)`) in the original endian. Yet if that one new number is sent to another machine, with the alternate endian, as a sequence of bytes, that other machine will see the original value. – chux - Reinstate Monica Sep 02 '16 at 17:40

2 Answers2

3

You can't change the endianness of the system in general (there are bi-endian architectures), this would require you to change the instruction set. You can change the endianness of the data you use though. Take a look at this question to see how.

Community
  • 1
  • 1
tversteeg
  • 4,717
  • 10
  • 42
  • 77
3

Endianess depends on the CPU hardware. So normally you can't do anything about it.

The code you have seen was most likely just tossing bytes around from one endianess to the other. Though some CPUs (for example some PowerPC) do have the possibility to configure endianess by writes to a hardware register.

Lundin
  • 195,001
  • 40
  • 254
  • 396