Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

bit string

by kiseok7 (Beadle)
on Jul 24, 2001 at 06:21 UTC ( #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? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2023-03-26 16:08 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (63 votes). Check out past polls.