Perl Basics
Introduction to Perl
FAQ's
CGI
Regular Expressions

PC Overview
Cool Stuff
My Modules
Success Stories
Links
Perl in the News
Logos

PC Internals
About
Contact
Handy Logos
What's new

Binary Arithmetics

See also
P-friendly

[ home ] - [ search ] - [ sitemap ]


up
#!/usr/bin/perl -w
use strict;

my $arg1 = 1111;
my $arg2 = 0001;

my $sum = &bin2dec($arg1) + &bin2dec($arg2);

printf("%08d\n", $arg1);
printf("%08d\n", $arg2);
printf("%08d\n", &dec2bin($sum));

sub bin2dec
{
    return unpack("N", pack("B32", substr("0" x 32 . shift, -32)));
}

sub dec2bin($) { return sprintf("%b", $_[0]) }

Output:

# 00001111
# 00000001
# 00010000
home - feedback - search

$Id: foo.htm,v 1.10 2004/03/11 17:50:01 reto Exp $
© 1998-2004 reto :)