@@ -569,8 +569,14 @@ bits efficiently, which is possible on most platforms; it is abstracted here as
569569
570570``` python
571571def count_trailing_zeros (v ):
572- """ For a non-zero value v, find z such that v=(d<<z) for some odd d."""
573- return (v & - v).bit_length() - 1
572+ """
573+ When v is zero, consider all N zero bits as "trailing".
574+ For a non-zero value v, find z such that v=(d<<z) for some odd d.
575+ """
576+ if v == 0 :
577+ return N
578+ else :
579+ return (v & - v).bit_length() - 1
574580
575581i = N # divsteps left to do
576582while True :
@@ -601,7 +607,7 @@ becomes negative, or when *i* reaches *0*. Combined, this is equivalent to addin
601607It is easy to find what that multiple is: we want a number * w* such that * g+w&thinsp ; f* has a few bottom
602608zero bits. If that number of bits is * L* , we want * g+w&thinsp ; f mod 2<sup >L</sup > = 0* , or * w = -g/f mod 2<sup >L</sup >* . Since * f*
603609is odd, such a * w* exists for any * L* . * L* cannot be more than * i* steps (as we'd finish the loop before
604- doing more) or more than * &eta ; +1* steps (as we'd run ` eta, f, g = -eta, g, f ` at that point), but
610+ doing more) or more than * &eta ; +1* steps (as we'd run ` eta, f, g = -eta, g, - f ` at that point), but
605611apart from that, we're only limited by the complexity of computing * w* .
606612
607613This code demonstrates how to cancel up to 4 bits per step:
@@ -618,7 +624,7 @@ while True:
618624 break
619625 # We know g is odd now
620626 if eta < 0 :
621- eta, f, g = - eta, g, f
627+ eta, f, g = - eta, g, - f
622628 # Compute limit on number of bits to cancel
623629 limit = min (min (eta + 1 , i), 4 )
624630 # Compute w = -g/f mod 2**limit, using the table value for -1/f mod 2**4. Note that f is
0 commit comments