The function bit_vec below has the same input signature as pack; it returns the list of 1s and 0s corresponding to the bits in the result of applying pack to its (bit_vec's) input. As an illustration, I also give a function, ieee_fp_bit_string, which represents IEEE floats, in an x86 architecture, using the same format as is used in this handy page.
sub bit_vec {
my ( $template, @rest ) = @_; # to conform to pack's proto
my $p = pack $template, @rest;
return map vec( $p, $_, 1 ), 0..( ( length $p ) * 8 ) - 1;
}
sub ieee_fp_bit_string {
my $f = shift;
my $template = shift || 'd';
die q(Template must be 'f' or 'd')
unless $template eq 'f' || $template eq 'd';
my @bits = reverse bit_vec( $template, $f );
my ( $e, $m ) = $template eq 'f' ? ( 8, 32 ) : ( 11, 64 );
return join ' ',
$bits[ 0 ],
join( '', @bits[ 1..$e ] ),
join( '', @bits[ $e+1..$m-1 ] );
}
sub show {
my $f = shift;
my $fs = sprintf( '%g', $f );
printf "%-12s: %s\n", $fs, ieee_fp_bit_string( $f, 'f' );
}
show( $_ ) for 4 * atan2( 1, 1 ), # pi
1,
1 + 2**-23, # 1 + POSIX::FLT_EPSILON
4,
4 - 2**-22,
2**126 * ( 4 - 2**-22 ), # largest IEEE float
2**-126, # smallest normalized IEEE flo
+at
2**-149; # smallest unnormalized IEEE f
+loat
__END__
3.14159 : 0 10000000 10010010000111111011011
1 : 0 01111111 00000000000000000000000
1 : 0 01111111 00000000000000000000001
4 : 0 10000001 00000000000000000000000
4 : 0 10000000 11111111111111111111111
3.40282e+38 : 0 11111110 11111111111111111111111
1.17549e-38 : 0 00000001 00000000000000000000000
1.4013e-45 : 0 00000000 00000000000000000000001