SPUG: The bit pattern of a number (was sum-of-squares)

John W. Krahn krahnj at acm.org
Sun Oct 5 16:38:05 CDT 2003


On Sunday 05 October 2003 10:51, Ben Reser wrote:
> On Sun, Oct 05, 2003 at 09:57:34AM -0700, John W. Krahn wrote:
> > $ 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
>
> pack/unpack doesen't return the internal representation, but rather
> the representation that you asked for via your format.  I.E. This
> output may not match the output that perl actually was storing the
> data in.  Which is what I think he was looking for.

It does if you use the correct formats:

$ perl -le'print unpack "B*", pack "S", 0b0001001000110100'
0011010000010010

That might be clearer using hexadecimal:

$ perl -le'print unpack "H*", pack "S", 0x1234'
3412

Which shows the little-endian representation on my Intel compatable
machine.


John
-- 
use Perl;
program
fulfillment




More information about the spug-list mailing list