Friday, January 30, 2015

Bit Reverse Perl



#!/usr/bin/perl
$n=8;
$m=log($n)/log(2);
for ($i=0; $i< $n; $i++)
{
    $b= dec2bin( $i );
    $b1=substr($b, 32-$m,32 );
    $b2= reverse(substr($b, 32-$m,32 ));
    $br=bin2dec($b2);
    print $i, "\t";
    print $b1, "\t";
    print $b2, "\t";   
    print $br, "\n";
}

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

No comments: