SPUG: The bit pattern of a number (was sum-of-squares)
John W. Krahn
krahnj at acm.org
Sun Oct 5 11:57:34 CDT 2003
On Sunday 05 October 2003 09:03, Mathew D. Watson wrote:
> I would like to determine the bit pattern of a Perl floating point
> number. In C I would do something like:
> 1. start by assigning the address of a double to a pointer-to-char;
> 2. then I'd look at the char value (i.e. determine its bit pattern);
> 3. increment the char pointer;
> 4. repeat steps 2 & 3 until I've reached the end of the double.
>
> How do I do this in Perl? Maybe this is really simple, but I'm
> stumped.
$ perl -le'print scalar reverse unpack "b*", pack "d", 1'
0011111111110000000000000000000000000000000000000000000000000000
$ perl -le'print scalar reverse unpack "b*", pack "d", -1'
1011111111110000000000000000000000000000000000000000000000000000
$ perl -le'print scalar reverse unpack "b*", pack "d", 1e25'
0100010100100000100010110010101000101100001010000000001010010001
$ perl -le'print scalar reverse unpack "b*", pack "d", 1e-25'
0011101010111110111100101101000011110101110110100111110111011001
John
--
use Perl;
program
fulfillment
More information about the spug-list
mailing list