Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-03-29 00:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found