Search code examples
c#doubleendiannessbitconverter

What is the endianess of a double on Windows 10 64bit?


I am using c# and writing a program to send numbers over UDP. I am on the Windows 10 64bit platform and I am using BitConverter in order to get the bytes from integers, doubles, etc..

As an example:

If I use:

Byte[] data = BitConverter.GetBytes((int)1);

I get, 01000000 in HEX, which would be little endian as expected.

If I use:

Byte[] data = BitConverter.GetBytes((double)1);

I get, 000000000000f03f in HEX, which looks like a big endian number but I am just not so sure.

My guess is I don't have a good understanding of endianess or of the double format. I suppose it is also possible that Windows stores doubles different from ints?


Solution

  • Certainly little-endian.

    Remember that IEEE floating-point is a bitfield, with sign having higher significance than exponent, which in turn has higher significance than mantissa.

    Your integer example has only one field, and its low bits are set.

    Your double example has all zero bits in the mantissa field, and the more significant field of exponent bits is non-zero. (Both of these are affected by the biasing used by IEEE-754)

    The significant bits are at the higher memory addresses, just like with the little-endian integer.

    For reference, IEEE-754 for 1.0 is { sign: 0, exponent: 0x3ff, mantissa: 0x0000000000000 }