# Binary manipulation of a floating point number

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Hello all,

I'd like to split a 64-bits word (hold as a floating point number) into
two 32-bits words (hold as integers). Bitwise operators are working on
Integers, not on floating point.

Anyone can tell me how I can do this?

Yannick

## Re: Binary manipulation of a floating point number

vendredi5h@gmail.com wrote:

To get the upper half, divide the float by 4294967296 and floor it. To
get the lower half, get the float's modulus 4294967296 and floor it.

## Re: Binary manipulation of a floating point number

vendredi5h@gmail.com wrote:

You could do it with a C extension.  What are you actually trying to do?
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

## Re: Binary manipulation of a floating point number

Tim Roberts a écrit :

Ok.

That's a kind of experimentation of a floating point dissection. As
with rats in high school! :o) But the goal is to get the n most
significant bits of a floating point fraction part and keep them for
futur use. Any other suggestion?

I would prefer PHP but I can do this little program in C too.

Yannick

## Re: Binary manipulation of a floating point number

Perhaps the binary<->decimal functions would be of use:

http://us3.php.net/manual/en/function.decbin.php
http://us3.php.net/manual/en/function.bindec.php
http://us3.php.net/manual/en/function.base-convert.php

Convert it to a binary string, cut it in half, then pad with zeroes
(don't forget to sign extend if you need to).

Also, bitwise operators don't work on floating point numbers in PHP?  I
don't have access to PHP right now, but I find that odd.

vendredi5h@gmail.com wrote:

## Re: Binary manipulation of a floating point number

No.  All of those function are defined as accepting integers.  PHP will
helpfully convert the floating value to int before performing the
conversion.

No.  The bitwise operators are ALSO defined as accepting integers.

PHP was just not designed for doing this kind of hackery.  It makes it easy
to do the things that people really need to do.

On the other hand, C won't do this, either.  This results in a compilation
error:

double d = 6.0;
return d & 0xffff0000;

And this does the same thing as PHP -- it converts to int first:

return (int)d & 0xffff0000;

You have to either use a union, or convert it as a pointer:

return (*(int*)&d) & 0xffff0000;
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

## Re: Binary manipulation of a floating point number

This is just a guess but manipulating it with pack and unpack just might
work, something like this:
print_r(unpack('Lhigh/Llow', pack('d',12.015)));

first pack the 64bit double into a string and then read two 32bit longs from
the packed string. Just an idea. Altough when I tested it with 12.015 didn't
seem anything like 12 and 015 after unpacking it, so it might not work at
all but give it a try...

--
"ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
spam@outolempi.net | Gedoon-S @ IRCnet | rot13(xvzzb@bhgbyrzcv.arg)

## Re: Binary manipulation of a floating point number

I did a little more testing and by golly, it seems actually to be working, I
get the same result out what goes in

\$x = unpack('L2long', pack('d',999999.999999));
\$y = unpack('ddouble', pack('L2',\$x['long1'],\$x['long2']));

print_r(\$x);
print_r(\$y);

So apparently the pack and it's evil twin sister unpack offer a workaround
for splitting numbers to high and low end.

--
"ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
spam@outolempi.net | Gedoon-S @ IRCnet | rot13(xvzzb@bhgbyrzcv.arg)

## Re: Binary manipulation of a floating point number

Kimmo Laine a écrit :

, I

Hello Kimmo,

I'll have to look at both functions syntax in order to understand how
it works, but it works!

Thanks for that.

And I agree with Tom when he says that PHP has not been created for
that kind of work. Doing that in C, I used an union to reach
float/double's bits.