How can I do bit operation on python float value

John Machin sjmachin at
Mon Mar 23 08:18:45 CET 2009

On Mar 23, 5:44 pm, valpa <valpass... at> wrote:
> I have a python float 1.2345678. I know that it is stored as a double
> in C type. And I know it actually is 1010101010101 -like format. Then
> I want to do some bit operation on it. How?
> Sure, I want a float output when I finish the operation.

import struct
pack it into a str [Python 2.X] using d format
unpack it using Q format, gets you a 64-bit unsigned int
do "some bit operation" using & | ^ ~ << >> operators
pack it into a str using Q format
unpack it into a float using d format

What is "some bit operation"? Perhaps there is already a high-level
way of doing some of what you want e.g. in the math module: frexp,
ldexp, isnan, isinf, ...


More information about the Python-list mailing list