After having searched for a while and trying to figure it out, I am at the mercy of Perl Monks. Here is the code that should effectively right rotate $p, bitwise, by 1 spot each time through the loop:

for($a = 0; $a < ($n % $SIZE); $a++) { $carry = 0x1 & $p; #extract least significant bit $p >>= 1; #shift $p by one spot to the right if($carry == 1) #to rotate in a leading 1 { my $or = 2 ** ($SIZE - 1); #$SIZE is # of bits in $p print "OR: $or\n"; print "pre|: $p. carry: $carry\n"; $p |= $or; #bitwise or in $or to add the leading 1 to $p } print "$a-$p\n"; }
I am using Perl 5.6.1. Here are some sample outputs for a given $p.

#this case works when $SIZE = 32 and $n = 3
p: 204229738. #ok
0-102114869 #ok, carry was 0 before 1st shift
OR: 2147483648 #carry is 1 before 2nd shift
pre|: 51057434. carry: 1 #p after second shift
1-2198541082 #p after | with $or
2-1099270541 #p after third shift

#this case fails when $SIZE = 40 and $n = 1
p: 418262508595. #ok
OR: 549755813888 #ok
pre|: 2147483647. carry: 1 #not ok
0-4294967295 #not ok

It seems that anything of size 5 bytes or greater fails consistently. Any thoughts why and how to combat this?


In reply to Bitwise ops inconsistent by smrtdrmmr

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.