in reply to Bit manipulation of a bit stream to generate different elements of an array with each nibble taking either 0 or 1 in perl

Okay, so here is the code. I inserted a couple of print statements so we can see what it does. I am still not sure I understand the logic behind this script.
#!/usr/bin/perl use strict; use warnings; # To generate the stream of all 1s sub GenerateStream { my $width = shift; my $i; my $j; my $k; my $temp_num = 0; my $numofstrobes = 4; my @strobes; my $all1stream="1" x $width; my $x_num = oct("0b" . $all1stream); # to convert string into int print "\nx_num=$x_num"; # To Calculate the number of streams for($i=4;$i<=$width;$i=$i+4){ $numofstrobes+=int($width/$i); } print "\nwidth=$width; numofstrobes=$numofstrobes; i=$i\n"; #To calculate the bit stream $j=4; for($i=0; $i<$numofstrobes-1; $i++){ my $k=$j-4; my $temp_num=$x_num; while($k<$j) { $temp_num=$temp_num-(2**$k); $k++; } $strobes[$i]=$temp_num; $j+=4; $strobes[$numofstrobes-1]=$x_num; } return @strobes; } my @X; for (my $i = 0; $i < 12; $i++) { @X = GenerateStream($i); foreach my $L (@X) { printf "\n%0.32b", $L; } }
  • Comment on Re: Bit manipulation of a bit stream to generate different elements of an array with each nibble taking either 0 or 1 in perl
  • Download Code