Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

bit string

by kiseok7 (Beadle)
on Jul 24, 2001 at 06:21 UTC ( [id://99218]=perlquestion: print w/replies, xml ) Need Help??

kiseok7 has asked for the wisdom of the Perl Monks concerning the following question:

Replies are listed 'Best First'.
Re: bit string
by I0 (Priest) on Jul 24, 2001 at 07:39 UTC
    print unpack"B8", pack "C",$a;
Re: bit string
by Zaxo (Archbishop) on Jul 24, 2001 at 07:06 UTC

    Binary printf formats aren't supported in earlier versions. From v5.6.0 perldoc perldelta:

    "Binary numbers supported

     Binary numbers are now supported as literals, in s?printf formats, and `oct()':

         $answer = 0b101010;
         printf "The answer is: %b\n", oct("0b101010");"

    Update: forgot to put in the meat.
    As a string:

    $ perl -e '$c = 12;print reverse map {vec $c,$_,1} 0..8*length($c)-1; +' 0011001000110001

    As a number:

    $ perl -e '$c = 12;print reverse map {$c&1<<$_?1:0} 0..8;' 000001100

    After Compline,

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://99218]
Approved by root
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (1)
As of 2024-07-21 14:27 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.