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

In reply to Visualize packed data as bits by tlm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.