i formulate infinity and store it deep inside of me

Powered by Astir.News engine

Perl routines

10 марта 2011, 22:16

Краткий листинг часто используемых функций для hackchallenges - включая работу с ASCII последовательностью длинных строковых бинарных. Будет пополняться.

#!/usr/bin/perl

sub bin2dec {return unpack("N", pack("B32", substr("0" x 32 . shift, -32)));}
sub bin2hex {return sprintf("0x%x", bin2dec(shift));}
sub dec2hex {return sprintf("%x", shift);}
sub dec2bin {return unpack("B32", pack("N", shift));}
sub dec2bin16 {return substr(dec2bin(shift),24,8);}
sub hex2dec($) {return hex $_[0];}

sub XOR {my ($s1,$s2)=@_; $l=length($s1); my @s1=split//,$s1; my @s2=split//,$s2; $cnt=0; my $res=""; foreach my $t(@s1) {if ($t eq "0" and $s2[$cnt] eq "0") {$r="0";} elsif ($t eq "1" and $s2[$cnt] eq "1") {$r="0";} else {$r="1";} $res.=$r; $cnt++;} return $res;}

sub AND {my ($s1,$s2)=@_; $l=length($s1); my @s1=split//,$s1; my @s2=split//,$s2; $cnt=0; my $res=""; foreach my $t(@s1) {if ($t eq "1" and $s2[$cnt] eq "1") {$r="1";} else {$r="0";} $res.=$r; $cnt++;} return $res;}

sub  OR {my ($s1,$s2)=@_; $l=length($s1); my @s1=split//,$s1; my @s2=split//,$s2; $cnt=0; my $res=""; foreach my $t(@s1) {if ($t eq "0" and $s2[$cnt] eq "0") {$r="0";} else {$r="1";} $res.=$r; $cnt++;} return $res;}

sub NOT {my ($s1,$s2)=@_; $l=length($s1); my @s1=split//,$s1; my @s2=split//,$s2; $cnt=0; my $res=""; foreach my $t(@s1) {if ($t eq "0" and $s2[$cnt] eq "0") {$r="0";} elsif ($t eq "1" or  $s2[$cnt] eq "1") {$r="1";} $res.=$r; $cnt++;}return $res;}

sub INV {my ($s1)=@_; $l=length($s1); my @s1=split//,$s1; $cnt=0; my $res=""; foreach my $t(@s1) {if ($t eq "0") {$r="1";} else {$r="0";} $res.=$r; $cnt++;}return $res;}

sub REV {my ($s1)=@_; $l=length($s1); my @s1=split//,$s1; $cnt=0; my $res=""; foreach my $t(reverse @s1) {$res.=$t;} return $res;}

sub REV2 {return reverse(shift);} # :))

#-------------------------
$file=$ARGV[0] || "_out.0"; $l=-s $file;
open (F,"<$file"); binmode(F); $bin='';  seek(F,0,1); read(F, $bin, $l); close (F);
#-------------------------
use Math::BigInt;
$x = Math::BigInt->new(0);
$x->badd($y)
#-------------------------