Use oct to go from bitstrings to numbers and sprintf to go from a number to a bitstring. As for the math, the usual operators work once you've done the conversion.
#!/usr/bin/perl
use strict;
use warnings;
my $bitstring1 = shift @ARGV || '10010010111';
my $bitstring2 = shift @ARGV || '01101011010';
my $number1 = oct "0b$bitstring1";
my $number2 = oct "0b$bitstring2";
printf "%s + %s = %b\n", $bitstring1, $bitstring2, $number1 + $number2
+;
Update
As BrowserUK mentioned, this will only get you as far as your platform's integer types allow. Fortunately, Math::BigInt also accepts the '0b' prefix for input and has the as_bin method for output, and has replacements for oodles of math operations.
-
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.
|