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

Re: Handling bits in Perl

by ikegami (Patriarch)
on Jul 25, 2006 at 14:41 UTC ( [id://563535]=note: print w/replies, xml ) Need Help??


in reply to Handling bits in Perl

The same bit operations available to C (&, |, ^, << and >>) are available to Perl. Perl also has vec. If you plan on doing some serious number crunching, you could use PDL or interface to C code via XS or Inline::C.

Replies are listed 'Best First'.
Re^2: Handling bits in Perl
by Thelonius (Priest) on Jul 25, 2006 at 15:30 UTC
    If ikegami's excellent suggestions aren't enough, there's also a Bit::Vector module on CPAN.

    One nice thing about the bitwise operators in Perl is that you can use them on whole strings, not just on integers. For example,

    use strict; my $x = "abcdefghijklmnopqrstuvwxyz"; my $y = ("A" ^ "a") x length($x); my $z = $x & ~$y; print "\$z = $z\n";
    This outputs $z = ABCDEFGHIJKLMNOPQRSTUVWXYZ. (This is just an example, not recommended for case conversion because it screws up non-alphabetic characters.)

    You should also be aware of pack/unpack "H*", pack/unpack "B*", or "b*, to print bit strings for debugging, to input long constants, etc.

      Unforunately, you can't use << and >> on strings :(
        Good point!
        That would be cool, though.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://563535]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (1)
As of 2024-07-22 01:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    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.