Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Binary Math?

by BrowserUk (Patriarch)
on Sep 30, 2003 at 22:07 UTC ( [id://295461]=note: print w/replies, xml ) Need Help??


in reply to Binary Math?

I'm not aware of any modules for this, but I never looked as perl makes it so easy to this yourself.

#! perl -slw use strict; sub b2n{ eval '0b'. $_[0] } sub n2b{ sprintf '%b', $_[0] } my( $bs1, $bs2 ) = ( '0100100100101', '1010101010100' ); print "$bs1 + $bs2 = ", n2b( b2n( $bs1 ) + b2n( $bs2 ) ); __END__ P:\test>295453 0100100100101 + 1010101010100 = 1111001111001

If your bitstrings are longer than 32 bits (or 64 if your lucky enough to be using a 64-bit platform) then you might need something more elaborate.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-25 15:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found