Byte Swapping Floating Point Types

The standard library functions only have byte swapping functions for 16 bit and 32 bit integral types. 8 bit data doesn't need swapping. But sometimes you want to write floating point data to the network or a file. This is problematic in that different processor architectures may use different bit level representations of floating point data, but these days most machines use IEEE 754 implementations that are mostly compatible. Assume for this article we are not concerned with this level of compatibility. (But don't assume it for your application! if you are sending/storing doubles and floats, then it behooves you to understand the platforms you care about).

The C standard library doesn't have native functions for byte swapping 8 byte double, or for that matter 32 bit float data types. So almost everyone has to code up their own swapping routines one way or the other. It turns out that a naive implementation of byte swapping floats can lead to subtle errors, which is what this article is about.

http://www.dmh2000.com/cpp/dswap.shtml

Comments

Popular Posts