12

For integer values, it is pretty straightforward the difference in little endian and big endian representation.

But it is not clear for me how a little endian float differs from a big endian float.

And finally, I would like to know which is more commonly used.

5
  • 11
    The endianness affects the way the values are represented in memory for all multi-byte types. Floats are no different than 32-bit integers in this case. Commented Jan 29, 2012 at 20:19
  • The 4 bytes are just stored in reverse order. Commented Jan 29, 2012 at 20:58
  • 4
    What confuses me is that the floating point representation isn't splited in bytes (like an integer). Sign is 1 bit, exponent 8 bits and significand is 23. Reversing the order would just make it harder to access the values. Commented Jan 29, 2012 at 21:07
  • 5
    André, you're talking about totally different things. byte order is a layer below the numeric representation. Integers and floating-point numbers don't care about endianness; they're just multi-byte values. Likewise, endianness doesn't care about whether it's an int or a float. It just stores the bytes in a particular order.
    – Joey
    Commented Jan 30, 2012 at 7:34
  • @HansPassant "The 4 bytes are just stored in reverse order. "? How to understand this? Could you please explian that in more detail for me?
    – John
    Commented May 22, 2021 at 1:41

2 Answers 2

18

Endianness just is a property of the bytes that make up a value that's composed from multiple bytes. Since a floating point number takes up 4 or 8 bytes endianness tells you in which order to read them. This is exactly the same as with integer values.

8

Some sources say IEEE754 floats are always stored little-endian but The IEEE754 specification for floating point numbers simply doesn't cover the endianness problem and may vary from machine to machine. Here is sample code for floating point / byte array conversion:

#include <stdio.h>

int main(int argc, char** argv){
  char *a;
  float f = 3.14159;  // number to start with

  a = (char *)&f;   // point a to f's location

  // print float & byte array as hex
  printf("float: %f\n", f);
  printf("byte array: %hhX:%hhX:%hhX:%hhX\n", \
    a[0], a[1], a[2], a[3]);

  // toggle the sign of f -- using the byte array
  a[3] = ((unsigned int)a[3]) ^ 128;

  //print the numbers again
  printf("float: %f\n", f);
  printf("byte array: %hhX:%hhX:%hhX:%hhX\n", \
    a[0], a[1], a[2], a[3]);

  return 0;
}

It's output on a little-indian machine:

float: 3.141590 byte array: D0:F:49:40 float: -3.141590 byte array: D0:F:49:C0

Theoretically, on a big-endian machine the order of bytes would be reversed.

Reference: http://betterexplained.com/articles/understanding-big-and-little-endian-byte-order/

2
  • 4
    On a SPARC machine: float: 3.141590, byte array: 40:49:F:D0; float: 3.141560, byte array: 40:49:F:50
    – automaciej
    Commented Mar 29, 2012 at 11:49
  • This is exactly how implemented it!
    – Owl
    Commented Jul 29, 2016 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.