Bit twiddling floating point numbers
Grant Edwards
grante at visi.com
Wed Mar 5 15:38:21 EST 2008
On 2008-03-05, Jeff.Goldfinkle at gmail.com <Jeff.Goldfinkle at gmail.com> wrote:
> Is there a simple way to twiddle the bits of a float? In particular, I
> would like to round my float to the n most significant bits.
>
> For example - 0.123 in binary is 0.000111111
> Rounding to 4 bits I get 0.0001.
>
> I can pack and unpack a float into a long
> e.g.
> struct.unpack('I',struct.pack('f',0.123))[0]
> but then I'm not sure how to work with the resulting long.
>
> Any suggestions?
Just use the bitwise and/or/not operators: & | ~
--
Grant Edwards grante Yow! Half a mind is a
at terrible thing to waste!
visi.com
More information about the Python-list
mailing list