I prototype a lot of my C microcontroller code in Perl, and for that purpose so I, and others might benefit, I wrote Bit::Manip. That contains code that has to be compiled, so for a Pure Perl version, there's Bit::Manip::PP.
It does all the things that we who have to do bitwise operations from time-to-time but not as a profession (or often enough to remember how) we need to do.
Here's an example of turning 'off' a bit:
Code:
use strict;
use warnings;
use Bit::Manip qw(:all);
my $num = 0b11100111; # or literal 231
my $bit_position = 6;
my $flipped = bit_off($num, $bit_position);
printf("orig: %b\n", $num);
printf("flipped: %b\n", $flipped);
Output:
steve@maezi ~/scratch >perl bit.pl
orig: 11100111
flipped: 10100111
-
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.
|