In Python 2.5, I have a float and I’d like to obtain and manipulate its bit pattern as an integer.
For example, suppose I have
x = 173.3125
In IEEE 754 format, x‘s bit pattern in hexadecimal is 432D5000.
How can I obtain & manipulate (e.g., perform bitwise operations) on that bit pattern?
You can get the string you want (apparently implying a big-endian, 32-bit representation; Python internally uses the native endianity and 64-bits for floats) with the
structmodule:this doesn’t yet let you perform bitwise operations, but you can then use struct again to map the string into an int:
and now you have an
intwhich you can use in any sort of bitwise operations (follow the same two steps in reverse if after said operations you need to get afloatagain).