Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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.


In reply to Re: Binary Math? by athomason
in thread Binary Math? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    [1nickt]: marioroy Yes, I am using it with MCE, as is Discipulus I believe. I was trying to work out how to make a cpanfile that would be smart enough to know which deps to require.
    [1nickt]: See this code. (I expected to simply eval loading threads as a check, but weirdness happened with Perlbrew so it's a grep of -V ...)
    [choroba]: Config might be better than grepping -V
    [Corion]: Also see Config::V, which is less of that hackery, or that hackery hidden in a module ;)
    [1nickt]: The problem was with Perlbrew
    [Corion]: Whoops - Config::Perl::V
    [1nickt]: I found that when using Perlbrew as recommended, with cpanminus in the system perl lib, such tests were failing to detect the data about the perl that was the install destination.

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (14)
    As of 2017-10-18 15:33 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My fridge is mostly full of:

















      Results (249 votes). Check out past polls.

      Notices?