I have a loop from 0 to 127. I need to do the following in perl => 1. convert this decimal number (0 to 127) into a binary string of 8 bits 2. split this binary number into 2 bits each like "dd cc bb aa" 3. Check if any 2 consecutive bits is 0 i.e. either dd or cc or bb or aa is equal to zero and update a counter if they are. I tried the following code but it is not working =>
------------------------------- for ($v = 0; $v < 128; $v++) { $bin_num = sprintf("%0b", $v); # This will give me the binary num +ber if ($v < 64) { $cnt++; # Because uptil value 63 "dd" is always going to be +zero } else { if ($bin_num =~ /00/) { print "\n Coming here if string 00 found for value $bin_nu +m \n"; $cnt++; } else { print "\n Coming here if string 00 NOT found for value $bi +n_num \n"; } } -------------------------------
The problem with this code is that it increments the count even if binary value = 01 01 10 01 i.e. bb = 10 and aa = 01 I want it to increment the count only if aa || bb || cc || dd is zero It would have been easier if I could have somehow split the 8 bit binary number into 2 bits each in the format of dd cc bb aa However I don't know how to do that. Will unpack help here? Anywayz would be greatful if someone can help me with this. At the end of the loop the counter should have value 101 and right now it reaches count 107. Thanks.

In reply to binary string by alih110

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.