[Tutor] Binary fractions, recommendations?

Kent Johnson kent37 at tds.net
Thu Aug 3 19:09:08 CEST 2006


Liam Clarke wrote:
> Problem is; I can't bitshift on floats, so can't test. Can't bitshift
> on floats in C either.  Does anyone know how I could work on a binary
> representation of a float? Any language at all, I'm desperate...

In C you can certainly get access to the binary representation of a 
float as bytes. My C is really rusty but something like
float x = 1.3;
char* p = &x;

will give you a pointer to the binary representation of x. You can do 
something similar with the struct module in Python; struct.pack() will 
give you a byte string containing the representation of a float.

Kent



More information about the Tutor mailing list