Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Need help with pack

Reply
Thread Tools

Need help with pack

 
 
kj
Guest
Posts: n/a
 
      10-01-2005



I want to write the function to_bits that takes a floating point
number as argument and returns an array of 32 ones or zeroes,
corresponding to the bits of the input argument.

I have been staring at the docs for pack and unpack for a while
now trying to figure out how to use them to do this and I am as
clueless about it as I was when I first started. Also, perlpacktut
was no help either [1].

If someone could please tell me how to generate an array of one's
and zeros corresponding to the bits in a floating point number I'd
be very grateful.

kj

[1] Lest I appear very lazy or very stupid, I should point out that
I generally have *no problem* understanding the documentation for
Perl built-in functions. pack is a very exceptional case. It is
the only Perl function that I consider mysterious in the least.
I wish I could convey my bewilderment upon reading the docs for
pack. From the very first sentences it is as if I were reading
some English-like code in which all words mean something different
from what I think they should mean. As I said, the tutorial is no
help, because ultimately the tutorial just gives cookbook recipes
which I can follow, but I have no idea what I'm doing. If what I
need to do is not described in one of the recipes, then *I have no
idea* of how to devise my own recipe.
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      10-01-2005
kj wrote:
> I want to write the function to_bits that takes a floating point
> number as argument and returns an array of 32 ones or zeroes,
> corresponding to the bits of the input argument.
>
> I have been staring at the docs for pack and unpack for a while
> now trying to figure out how to use them to do this and I am as
> clueless about it as I was when I first started. Also, perlpacktut
> was no help either [1].
>
> If someone could please tell me how to generate an array of one's
> and zeros corresponding to the bits in a floating point number I'd
> be very grateful.



$ perl -le' print for unpack q/a/ x 32, unpack q/b*/, pack q/f/, 5.678 '
1
0
1
1
0
1
0
0
0
1
0
0
1
1
0
1
1
0
1
0
1
1
0
1
0
0
0
0
0
0
1
0



John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
 
 
 
tlm
Guest
Posts: n/a
 
      10-01-2005

>I want to write the function to_bits that takes a floating point
>number as argument and returns an array of 32 ones or zeroes,
>corresponding to the bits of the input argument.


How about this (untested):

sub to_bits {
my $f = pack 'f', shift;
return reverse map vec( $f, $_, 1 ), 0 .. 31;
}

HTH,

tlm
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      10-01-2005
kj <(E-Mail Removed)> wrote in news:dhn4dh$bqd$(E-Mail Removed):

> I want to write the function to_bits that takes a floating point
> number as argument and returns an array of 32 ones or zeroes,
> corresponding to the bits of the input argument.
>
> I have been staring at the docs for pack and unpack for a while
> now trying to figure out how to use them to do this and I am as
> clueless about it as I was when I first started. Also, perlpacktut
> was no help either [1].
>
> If someone could please tell me how to generate an array of one's
> and zeros corresponding to the bits in a floating point number I'd
> be very grateful.


#!/usr/bin/perl

use strict;
use warnings;

print unpack 'b*', pack 'f', 3.45;


Sinan

--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      10-02-2005
kj <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
>
>
> I want to write the function to_bits that takes a floating point
> number as argument and returns an array of 32 ones or zeroes,
> corresponding to the bits of the input argument.
>
> I have been staring at the docs for pack and unpack for a while
> now trying to figure out how to use them to do this and I am as
> clueless about it as I was when I first started. Also, perlpacktut
> was no help either [1].
>
> If someone could please tell me how to generate an array of one's
> and zeros corresponding to the bits in a floating point number I'd
> be very grateful.


I'll show how to generate a string (not an array) of zeroes and ones.

You need two steps. First use the "f" template (single-precision float)
to get the four bytes that make up a 32-bit float into a string. If
the number is in $_:

my $str = pack 'f', $_;

Next, unpack the result as a bit string, using the "b" template

my $bits = unpack 'b32', $str;

Run it for a few examples

printf "%.3f -> %s\n", $_, unpack 'b32', pack 'f', $_ for
1/8, 1/4, 1/2, 1, 2, 4, 8;

and see if you can make heads or tails of the output. The bytes may not
be in the expected order. On my machine, I get:

0.125 -> 01111100000000000000000000000000
0.250 -> 01111100000000010000000000000000
0.500 -> 11111100000000000000000000000000
1.000 -> 11111100000000010000000000000000
2.000 -> 00000010000000000000000000000000
4.000 -> 00000010000000010000000000000000
8.000 -> 10000010000000000000000000000000

If you want an array, either split the result in single characters, or
use

map vec( $str, $_, 1), 0 .. 31

instead of unpacking with "b".

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
kj
Guest
Posts: n/a
 
      10-02-2005
In <dhn80f$4vn$(E-Mail Removed)-Berlin.DE> http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Anno Siegel) writes:

>kj <(E-Mail Removed)> wrote in comp.lang.perl.misc:


>I'll show how to generate a string (not an array) of zeroes and ones.


>You need two steps. First use the "f" template (single-precision float)
>to get the four bytes that make up a 32-bit float into a string. If
>the number is in $_:


> my $str = pack 'f', $_;


>Next, unpack the result as a bit string, using the "b" template


> my $bits = unpack 'b32', $str;


>Run it for a few examples


> printf "%.3f -> %s\n", $_, unpack 'b32', pack 'f', $_ for
> 1/8, 1/4, 1/2, 1, 2, 4, 8;


>and see if you can make heads or tails of the output. The bytes may not
>be in the expected order. On my machine, I get:


>0.125 -> 01111100000000000000000000000000
>0.250 -> 01111100000000010000000000000000
>0.500 -> 11111100000000000000000000000000
>1.000 -> 11111100000000010000000000000000
>2.000 -> 00000010000000000000000000000000
>4.000 -> 00000010000000010000000000000000
>8.000 -> 10000010000000000000000000000000


>If you want an array, either split the result in single characters, or
>use


> map vec( $str, $_, 1), 0 .. 31


>instead of unpacking with "b".



Thank you very much. That was very helpful.

BTW, I was surprised to discover that the vec alternative is about
2.5x faster than unpack+split:

0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1.1. 1.1.1.1.1.0.0
0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1.1. 1.1.1.1.1.0.0
Rate unp vec
unp 26305/s -- -61%
vec 67622/s 157% --

I give the code below.

kj


use Benchmark 'cmpthese';

$::f = pack 'f', 1;

my $subs = {vec => 'map vec($::f, $_, 1), 0..31',
unp => 'split "", unpack "b32", $::f'};

# check that both work and produce the same result
{
local ($\, $,) = ("\n", '.');
print eval('sub {'.$subs->{ $_ }.'}')->(), "\n" for qw(vec unp);
}

# benchmark
cmpthese -1, $subs;

__END__
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
When Is A Visaster Service Pack Not A Service Pack? Lawrence D'Oliveiro NZ Computing 5 06-26-2008 09:11 PM
Buy 1 pack or 3 pack? (XP Pro x64) Rich Olver Windows 64bit 11 11-25-2006 11:33 PM
Excellent discount software packs - ImTOO Ripper Pack Platinum and ImTOO Ripper Pack Gold zhangelf01@gmail.com DVD Video 6 09-17-2006 03:27 AM
Pack parent control in child using control.pack(in_= syntax? Tim Jones Python 0 01-31-2004 10:22 PM
Poor Mans NIMH Battery Pack and SLA Battery Pack ajacobs2 Digital Photography 0 08-19-2003 12:42 PM



Advertisments