Extracting bit fields from an IEEE-784 float

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Jul 29 20:44:04 EDT 2012


I wish to extract the bit fields from a Python float, call it x. First I 
cast the float to 8-bytes:

s = struct.pack('=d', x)
i = struct.unpack('=q', s)[0]

Then I extract the bit fields from the int, e.g. to grab the sign bit:

(i & 0x8000000000000000) >> 63


Questions:

1) Are there any known implementations or platforms where Python floats 
are not C doubles? If so, what are they?

2) If the platform byte-order is reversed, do I need to take any special 
action? I don't think I do, because even though the float is reversed, so 
will be the bit mask. Is this correct?

3) Any other problems with the way I am doing this?



Thanks in advance,



Steven



More information about the Python-list mailing list