Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

by harangzsolt33 (Chaplain)
on Nov 16, 2018 at 07:02 UTC ( [id://1225892]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-23 18:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found