in reply to Bitwise operators

Ok here's what I've done, which works -
#!/usr/bin/perl #converter.plx use warnings; use strict; print "Please input a decimal no. to convert to binary : "; chomp (my $value = <STDIN>); print "$value & 1", "$Value & 2", "$value & 4"; print "$value & 8", "$value & 16", "$value & 32"; print "$value & 64", "$value & 128\n";
but I've started the next chapter which is on arrays and hashes, so although I'm not sure how to do it yet, I'm thinking there's a probably a shorter way? Paul.

Replies are listed 'Best First'.
Re^2: Bitwise operators
by liverpole (Monsignor) on Dec 25, 2006 at 19:34 UTC
    Hi again, thevoid,

    I just now read this earlier post of yours.

        print "$value & 1", "$Value & 2", "$value & 4";

    The problem you're having is that you can only interpolate a certain amount within strings, and doing bit-wise arithmetic is a little too much.

    You could do this if you used printf like so:
    printf "%d, %d, %d, ", $value & 1, $value & 2, $value & 4;

    And you could even convert to one or zero values by applying the appropriate shifts:

    printf "%d, %d, %d, ", ($value & 1), ($value & 2) >> 1, ($value & 4) > +> 2;

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      Hi, just spotted this!

      That's pretty nifty also : )