smrtdrmmr has asked for the wisdom of the Perl Monks concerning the following question:
I am using Perl 5.6.1. Here are some sample outputs for a given $p.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"; }
#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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Bitwise ops inconsistent
by Zaxo (Archbishop) on Aug 24, 2005 at 00:21 UTC | |
by ysth (Canon) on Aug 24, 2005 at 05:00 UTC | |
|
Re: Bitwise ops inconsistent
by pg (Canon) on Aug 24, 2005 at 01:26 UTC | |
|
Re: Bitwise ops inconsistent
by GrandFather (Saint) on Aug 24, 2005 at 00:22 UTC | |
|
Re: Bitwise ops inconsistent
by QM (Parson) on Aug 24, 2005 at 15:31 UTC |