How can I do bit operation on python float value

valpa valpassing at
Mon Mar 23 09:44:40 CET 2009

On 3月23日, 上午3时18分, John Machin <sjmac... at> wrote:
> 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, ...
> John

Thanks John!

Yes, I want to do a & operation.
The pack way is to convert float to/from string, Is it efficient?


More information about the Python-list mailing list